views:

568

answers:

5

Hi there

I have a function inside of a view function inside of a model class in the model.php file that looks like this

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)

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

A: 

I was able to fix the problem by storing the function in the appController.php and called the funtion when need be using

$this->sqlToUnix($SQLDate);

Sorry about asking the question but I just remembered the appController when I posted this ><

A: 

For your specific function, are you sure there are no builtin functions which return your UnixTime format?

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);
Karsten
Where would you put the MyHelpers class file?
ianmjones
+1  A: 

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();

neilcrookes
A: 

You can write a function is bootstrap.php (although you'd do better to include another PHP file from bootstrap.php instead).

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:

require_once(APP.'config'.DS.'my_file_of_whizzy_functions.php');

The function will then be available throughout your CakePHP app.

Aside from that, does strtotime($sqlDate); not convert a SQL time to a unix timestamp?

Chris Hawes
this is not necessary, because you can easily import any controller action, helper oder component to any controller, model, component or what ever, if you use cakephp.Re-writing the bootstrap is really not necessary for such minimalistic things like "sql2unix", due to the fact, that this function is NOT needed through out the whole code. But nice solution anyway ;)
daemonfire300
A: 

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.

daemonfire300