I have a function inside of a view function inside of a model class in the model.php file that looks like this
我在model.php文件中的模型类内部的视图函数中有一个函数,如下所示
function sqlToUnix($date){
$YMDThenHMS = explode(" ", $date);
$YMD = explode("-", $YMDThenHMS[0]);
$HMS = explode(":", $YMDThenHMS[1]);
$UnixTime = mktime($HMS[0], $HMS[1], $HMS[2], $YMD[1], $YMD[2], $YMD[0]);
return $UnixTime;
}
The problem is, when it returns $UnixTime, The return value is usable inside the model controller specific view function but it won't render my view (stops script propogation)
问题是,当它返回$ UnixTime时,返回值可在模型控制器特定的视图函数中使用,但它不会呈现我的视图(停止脚本传播)
Is there a place where I can build functions like this up for use ANYWHERE in ANY Controller?
有没有一个地方我可以在任何控制器中建立这样的功能以便在任何地方使用?
Such as the function time() built into PHP itself, I want to be able to use sqlToUnix anywhere
比如PHP本身内置的函数time(),我希望能够在任何地方使用sqlToUnix
5 个解决方案
#1
If you want to call this function from anywhere, i.e. in models, controllers, views, behaviors, components and helpers, you can put it in your app/config/bootstrap.php file. That's what it's for. Once the it's available globally simply as sqlToUnix();
如果要从任何地方(即模型,控制器,视图,行为,组件和帮助程序)调用此函数,可以将其放在app / config / bootstrap.php文件中。这就是它的用途。一旦它全局可用,就像sqlToUnix();
#2
For your specific function, are you sure there are no builtin functions which return your UnixTime format?
对于您的特定功能,您确定没有返回UnixTime格式的内置函数吗?
Is there a place where I can build functions like this up for use ANYWHERE in ANY Controller?
有没有一个地方我可以在任何控制器中建立这样的功能以便在任何地方使用?
class MyHelpers
{
public static function sqlToUnix($SQLDate)
{
// code
return $result;
}
}
// call me this way, anywhere:
$result = MyHelpers::sqlToUnix($SQLDate);
#3
You can write a function is bootstrap.php (although you'd do better to include another PHP file from bootstrap.php instead).
你可以编写一个函数是bootstrap.php(尽管你最好还是从bootstrap.php包含另一个PHP文件)。
I normally have any extra functions or configuration in a file within the /app/config directory and include it with in my bootstrap.php file:
我通常在/ app / config目录下的文件中有任何额外的功能或配置,并在我的bootstrap.php文件中包含它:
require_once(APP.'config'.DS.'my_file_of_whizzy_functions.php');
The function will then be available throughout your CakePHP app.
然后,该功能将在您的CakePHP应用程序中可用。
Aside from that, does strtotime($sqlDate); not convert a SQL time to a unix timestamp?
除此之外,strtotime($ sqlDate);不将SQL时间转换为unix时间戳?
#4
You can access this function via your controller and pass it into the view:
您可以通过控制器访问此功能并将其传递到视图中:
//Controller
//inside a controller action
{
$TIMESTAMP = $this->Model->sql2unix($this->Model->getTimestamp());
$this->set('timestampe',$TIMESTAMP);
// or does this even do not work?
}
otherwise you can create a component
否则你可以创建一个组件
//inside the component // inside of a component method
{
$MODEL = loadModel('ModelName');
$return = $MODEL->sql2unix($MODEL->getTimestamp());
return $return;
}
It is nearly "not important" where to place your code, you just have to follow cakephp's folder/class/helper/method/component structure. Read the manual part about components or helpers and you'll understand everything instantly.
放置代码几乎“不重要”,你只需要遵循cakephp的文件夹/ class / helper / method / component结构。阅读有关组件或帮助程序的手册部分,您将立即了解所有内容。
#5
I was able to fix the problem by storing the function in the appController.php and called the function when need be using
我能够通过将该函数存储在appController.php中来解决问题,并在需要时调用该函数
$this->sqlToUnix($SQLDate);
Sorry about asking the question but I just remembered the appController when I posted this ><
很抱歉提出这个问题,但是当我发布这个> <时,我只记得appcontroller< p>
#1
If you want to call this function from anywhere, i.e. in models, controllers, views, behaviors, components and helpers, you can put it in your app/config/bootstrap.php file. That's what it's for. Once the it's available globally simply as sqlToUnix();
如果要从任何地方(即模型,控制器,视图,行为,组件和帮助程序)调用此函数,可以将其放在app / config / bootstrap.php文件中。这就是它的用途。一旦它全局可用,就像sqlToUnix();
#2
For your specific function, are you sure there are no builtin functions which return your UnixTime format?
对于您的特定功能,您确定没有返回UnixTime格式的内置函数吗?
Is there a place where I can build functions like this up for use ANYWHERE in ANY Controller?
有没有一个地方我可以在任何控制器中建立这样的功能以便在任何地方使用?
class MyHelpers
{
public static function sqlToUnix($SQLDate)
{
// code
return $result;
}
}
// call me this way, anywhere:
$result = MyHelpers::sqlToUnix($SQLDate);
#3
You can write a function is bootstrap.php (although you'd do better to include another PHP file from bootstrap.php instead).
你可以编写一个函数是bootstrap.php(尽管你最好还是从bootstrap.php包含另一个PHP文件)。
I normally have any extra functions or configuration in a file within the /app/config directory and include it with in my bootstrap.php file:
我通常在/ app / config目录下的文件中有任何额外的功能或配置,并在我的bootstrap.php文件中包含它:
require_once(APP.'config'.DS.'my_file_of_whizzy_functions.php');
The function will then be available throughout your CakePHP app.
然后,该功能将在您的CakePHP应用程序中可用。
Aside from that, does strtotime($sqlDate); not convert a SQL time to a unix timestamp?
除此之外,strtotime($ sqlDate);不将SQL时间转换为unix时间戳?
#4
You can access this function via your controller and pass it into the view:
您可以通过控制器访问此功能并将其传递到视图中:
//Controller
//inside a controller action
{
$TIMESTAMP = $this->Model->sql2unix($this->Model->getTimestamp());
$this->set('timestampe',$TIMESTAMP);
// or does this even do not work?
}
otherwise you can create a component
否则你可以创建一个组件
//inside the component // inside of a component method
{
$MODEL = loadModel('ModelName');
$return = $MODEL->sql2unix($MODEL->getTimestamp());
return $return;
}
It is nearly "not important" where to place your code, you just have to follow cakephp's folder/class/helper/method/component structure. Read the manual part about components or helpers and you'll understand everything instantly.
放置代码几乎“不重要”,你只需要遵循cakephp的文件夹/ class / helper / method / component结构。阅读有关组件或帮助程序的手册部分,您将立即了解所有内容。
#5
I was able to fix the problem by storing the function in the appController.php and called the function when need be using
我能够通过将该函数存储在appController.php中来解决问题,并在需要时调用该函数
$this->sqlToUnix($SQLDate);
Sorry about asking the question but I just remembered the appController when I posted this ><
很抱歉提出这个问题,但是当我发布这个> <时,我只记得appcontroller< p>