Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Is there any way to include MySQL expressions like NOW() in the current build of ZF2 (2.0.0beta4) through Zend\Db and/or TableGateway insert()/update() statements?

Here is a related post on the mailing list, though it hasn't been answered: http://zend-framework-community.634137.n4.nabble.com/Zend-Db-Expr-and-Transactions-in-ZF2-Db-td4472944.html

It appears that ZF1 used something like:

    $data = array( 
        'update_time'   => new \Zend\Db\Expr("NOW()") 
    ); 

And after looking through Zend\Db\Sql\Expression I tried:

    $data = array(
        'update_time' => new \Zend\Db\Sql\Expression("NOW()"),
    );

But get the following error:

Catchable fatal error: Object of class Zend\Db\Sql\Expression could not be converted to string in /var/www/vendor/ZF2/library/Zend/Db/Adapter/Driver/Pdo/Statement.php on line 256

As recommended here: http://zend-framework-community.634137.n4.nabble.com/ZF2-beta3-Zend-Db-Sql-Insert-new-Expression-now-td4536197.html I'm currently just setting the date with PHP code, but I'd rather use the MySQL server as the single source of truth for date/times.

    $data = array(
        'update_time' => date('Y-m-d H:i:s'),
    );

Thanks, I appreciate any input!

share|improve this question

2 Answers

up vote 2 down vote accepted

I am not sure if they fixed this in the latest updates, but it was a know bug and even if they said it was fixed, for me it still didn't work after the fix.

share|improve this answer
Thanks for the response and link, I'll update to the latest source and see if it persists. – Aleross Jun 29 '12 at 10:58
zend framework 2.0 beta 6 and still no solution to this problem!?!?! – ufk Aug 30 '12 at 13:17
problem resolved in zend framework 2.0 beta 7 – ufk Sep 3 '12 at 12:58

Zend_Db_Sql_Expression works for me now.

$data = array(
    'update_time' => new \Zend\Db\Sql\Expression("NOW()"),
);
share|improve this answer
Would you mind adding a little more info around this answer? Thank you – Max May 7 at 3:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.