CodeIgniter Forums
Using Maria/MySQL Internal Time Functions in the Builder Class? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Using Maria/MySQL Internal Time Functions in the Builder Class? (/showthread.php?tid=77502)



Using Maria/MySQL Internal Time Functions in the Builder Class? - stlake2011 - 09-08-2020

Hi guys, 

Quick question, minor problem (I think)!

I am trying to update a 'DateTime' column in MariaDB/MySQL using the builder class but I keep getting this error:

Call to undefined function App\Models\now()  

I tried setting the update column using the following methods:

Method 1:

PHP Code:
$UpdateData = [
    'title' => $title,
    'slug' => $slug,
    'updated' => NOW(),
    'post' => $post
];
        
$builder
->where('slug',$slug);

$builder->update($UpdateData); 

Method 2:

PHP Code:
$builder->set('updated=NOW()');
$builder->where('slug',$slug);

$builder->update($UpdateData); 

Method 3: 

PHP Code:
$builder->set('updated',NOW());
$builder->where('slug',$slug);

$builder->update($UpdateData); 


Is there something I am missing here? Or is it impossible to work with Maria/MySQL's internal functions using the Builder Class?

No biggie if its impossible, as I can always resort to PHP Date/Time functions and have PHP do the work instead of MariaDB doing it. 
So this is more of a curiosity question as well as a serious one. 


RE: Using Maria/MySQL Internal Time Functions in the Builder Class? - InsiteFX - 09-09-2020

Because CodeIgniter no longer has the now() method.

Here I created a helper to do it, make sure to place the methods into a help file.

PHP Code:
/ -----------------------------------------------------------------------

/**
 * now () method
 */
if ( ! function_exists('now'))
{
    function 
now()
    {
        
// uses the default timezone.
        
return date_create('now')->format('Y-m-d H:i:s');
    }
}

// -----------------------------------------------------------------------

/**
 * nowTimeZone () method
 */
if ( ! function_exists('nowTimeZone'))
{
    function 
nowTimeZone($timeZone)
    {
        
// $timeZone format 'America/New_York'
        
return date_create('now'timezone_open($timeZone))->format('Y-m-d H:i:s');
    }




RE: Using Maria/MySQL Internal Time Functions in the Builder Class? - stlake2011 - 09-09-2020

(09-09-2020, 05:26 AM)InsiteFX Wrote: Because CodeIgniter no longer has the now() method.

Here I created a helper to do it, make sure to place the methods into a help file.

PHP Code:
/ -----------------------------------------------------------------------

/**
 * now () method
 */
if ( ! function_exists('now'))
{
    function 
now()
    {
        
// uses the default timezone.
        
return date_create('now')->format('Y-m-d H:i:s');
    }
}

// -----------------------------------------------------------------------

/**
 * nowTimeZone () method
 */
if ( ! function_exists('nowTimeZone'))
{
    function 
nowTimeZone($timeZone)
    {
        
// $timeZone format 'America/New_York'
        
return date_create('now'timezone_open($timeZone))->format('Y-m-d H:i:s');
    }


Thanks man for the helper, I figured there was something along those lines going on, but just wasn't sure what it was.


RE: Using Maria/MySQL Internal Time Functions in the Builder Class? - nfaiz - 09-09-2020

(09-08-2020, 03:40 PM)stlake2011 Wrote: Hi guys, 

Quick question, minor problem (I think)!

I am trying to update a 'DateTime' column in MariaDB/MySQL using the builder class but I keep getting this error:

Call to undefined function App\Models\now()  

I tried setting the update column using the following methods:

Method 1:

PHP Code:
$UpdateData = [
    'title' => $title,
    'slug' => $slug,
    'updated' => NOW(),
    'post' => $post
];
        
$builder
->where('slug',$slug);

$builder->update($UpdateData); 

Method 2:

PHP Code:
$builder->set('updated=NOW()');
$builder->where('slug',$slug);

$builder->update($UpdateData); 

Method 3: 

PHP Code:
$builder->set('updated',NOW());
$builder->where('slug',$slug);

$builder->update($UpdateData); 


Is there something I am missing here? Or is it impossible to work with Maria/MySQL's internal functions using the Builder Class?

No biggie if its impossible, as I can always resort to PHP Date/Time functions and have PHP do the work instead of MariaDB doing it. 
So this is more of a curiosity question as well as a serious one. 

How about

PHP Code:
$builder->set('updated''NOW()'false);
$builder->where('slug',$slug);

$builder->update($UpdateData); 



RE: Using Maria/MySQL Internal Time Functions in the Builder Class? - InsiteFX - 09-10-2020

CodeIgniter 3 had a now() method but it was dropped in CodeIgniter 4,
So I wrote them for CodeIgniter 4.


RE: Using Maria/MySQL Internal Time Functions in the Builder Class? - nfaiz - 09-10-2020

MySQL/MariaDb also using now(). We just need to add 3rd parameter as false.