Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use MySQL functions when building a query with Zend/PDO

I'm using Zend Framework with the PDO MySQL adapter and I want to use a function in my insert statement. Basically, the SQL I want to generate is this:

INSERT INTO `myTable` (`leftId`, `rightId`, `date`) VALUES ($left, $right, NOW())

This is the code in my Model:

$data = array(
    "leftId" => $left,
    "rightId" => $right,
    "date" => "NOW()"
);

$this->insert($data);

This tries to insert "NOW()" rather than NOW():

General error: 1292 Incorrect datetime value: 'NOW()' for column 'date' at row 1

How do I go about this?

like image 209
nickf Avatar asked Nov 28 '22 23:11

nickf


1 Answers

Found it in the manual*:

$data = array(
    "leftId" => $left,
    "rightId" => $right,
    "date" => new Zend_Db_Expr("NOW()")
);

*I know, can you believe it?

like image 62
nickf Avatar answered Dec 06 '22 15:12

nickf