简体   繁体   English

ZF2 Zend \\ Db使用Mysql表达式插入/更新(Zend \\ Db \\ Sql \\ Expression?)

[英]ZF2 Zend\Db Insert/Update Using Mysql Expression (Zend\Db\Sql\Expression?)

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? 有没有办法通过Zend \\ Db和/或TableGateway insert()/ update()语句在ZF2(2.0.0beta4)的当前版本中包含NOW()等MySQL表达式?

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 以下是邮件列表中的相关帖子,但尚未得到解答: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: 似乎ZF1使用了类似的东西:

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

And after looking through Zend\\Db\\Sql\\Expression I tried: 通过Zend \\ Db \\ Sql \\ Expression查看后我尝试了:

    $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. 按照这里的建议:http: //zend-framework-community.634137.n4.nabble.com/ZF2-beta3-Zend-Db-Sql-Insert-new-Expression-now-td4536197.html我目前正在设置使用PHP代码约会,但我宁愿使用MySQL服务器作为日期/时间的单一事实来源。

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

Thanks, I appreciate any input! 谢谢,我感谢任何输入!

Zend_Db_Sql_Expression works for me now. Zend_Db_Sql_Expression现在适用于我。

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

我不确定他们是否在最新的更新中解决了这个问题,但这是一个知道的错误 ,即使他们说它已修复,对我来说它仍然无法在修复后工作。

It works for me too. 它也适合我。 Maybe, your problem was like mine. 也许,你的问题就像我的一样。 The string error conversion was because the parameter that I passed to method was elements of array and not the array. 字符串错误转换是因为我传递给方法的参数是数组的元素而不是数组。 Check this and have sure that you are passing $object->method($array). 检查这一点并确保传递$ object-> method($ array)。

You can try: 你可以试试:

\Zend\Db\Sql\Predicate\Literal();
//or
\Zend\Db\Sql\Literal();
------------------------------------
$data = array(
    'update_time' => new \Zend\Db\Sql\Literal("NOW()"),
);

https://framework.zend.com/manual/2.2/en/modules/zend.db.sql.html#literal-literal https://framework.zend.com/manual/2.2/en/modules/zend.db.sql.html#literal-literal

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM