CodeIgniter Forums
Active Record and CURDATE() - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Active Record and CURDATE() (/showthread.php?tid=14793)



Active Record and CURDATE() - El Forum - 01-15-2009

[eluser]al404[/eluser]
i'm using active record for a query but i also would like to set a value of a filed to CURDATE() via SQL, it doesn't work eather with or without quote

how can i do that?

$data = array(
'title' => $title,
'datein' => 'CURDATE()',
other fields...
);

$this->db->update('news', $data);


Active Record and CURDATE() - El Forum - 01-15-2009

[eluser]meigwilym[/eluser]
I can't find anything in the user guide, but you could always try

Code:
$data = array(
  ‘title’ => $title,
  ‘datein’ => date('Y-m-d'),
  // other fields…
);
    
$this->db->update(‘news’, $data);

Mei


Active Record and CURDATE() - El Forum - 01-15-2009

[eluser]everdaniel[/eluser]
you might need to use the set() method to prevent CI from escaping your mysql function, so:

Code:
$this->db->set('title', $title);
$this->db->set('dateing', 'CURDATE()', FALSE);
$this->db->update('news');



Active Record and CURDATE() - El Forum - 01-15-2009

[eluser]al404[/eluser]
@meigwilym

that's may work but i may need in other occasion to use MySQL functions


Active Record and CURDATE() - El Forum - 01-15-2009

[eluser]kgill[/eluser]
If you want to use built-in functions you're pretty much going to have to abandon active record, anything you send is going to be quoted and treated as a string. Your other option is to modify the driver file and add a line to the _insert and _update functions to replace 'CURDATE()' with CURDATE().


Active Record and CURDATE() - El Forum - 01-15-2009

[eluser]everdaniel[/eluser]
[quote author="kgill" date="1232059522"]If you want to use built-in functions you're pretty much going to have to abandon active record, anything you send is going to be quoted and treated as a string. Your other option is to modify the driver file and add a line to the _insert and _update functions to replace 'CURDATE()' with CURDATE().[/quote]

You don't need to change any CI core file, as I said, you just need to use the set() method and pass FALSE as the third argument and that's it, CI won't quote the string.


Active Record and CURDATE() - El Forum - 01-15-2009

[eluser]kgill[/eluser]
Well now that's interesting... Now I have to figure out why the version of active record on our production system does not have that 3rd param. It was updated to 1.6.3 last year, someone is not going to be happy to see me.


Active Record and CURDATE() - El Forum - 01-15-2009

[eluser]al404[/eluser]
ok this way works fine

Code:
$query = array(
  ‘title1’ => $title1,
  ‘title2’ => $title2
);

$this->db->set($query);        
$this->db->set('datein', 'CURDATE()', FALSE);
      
$this->db->update(‘news’);