Welcome Guest, Not a member yet? Register   Sign In
Using Maria/MySQL Internal Time Functions in the Builder Class?
#1

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. 
Reply
#2

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');
    }

What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(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.
Reply
#4

(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); 
Reply
#5

CodeIgniter 3 had a now() method but it was dropped in CodeIgniter 4,
So I wrote them for CodeIgniter 4.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#6

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




Theme © iAndrew 2016 - Forum software by © MyBB