PHP Doctrine 1.2 SET 语句中的条件 IF 语句

Posted

技术标签:

【中文标题】PHP Doctrine 1.2 SET 语句中的条件 IF 语句【英文标题】:Conditional IF statement in a PHP Doctrine 1.2 SET statement 【发布时间】:2011-07-21 17:15:47 【问题描述】:

我想为 SET 行找到正确的语法:

Doctrine_Query::create()
->update('Media m')
->set('m.fallback IF(m.id = ?, 1, 0)', $id)  <-- not correct
->execute();

感谢您的帮助!

【问题讨论】:

我不认为 update 支持...实际上 update 不支持的不仅仅是简单的 where 子句,您必须求助于 RawSql 或使用 PDO。 【参考方案1】:

我认为这可以通过两个查询来完成:

Doctrine_Query::create()
    ->update('Media m')
    ->set('m.fallback', 1)
    ->where('m.id = ?', $id)
    ->execute();

Doctrine_Query::create()
    ->update('Media m')
    ->set('m.fallback', 0)
    ->where('m.id != ?', $id)
    ->execute();

我不知道它是否适合您,但至少可以满足您的需求。可能无法开箱即用,因为我手头没有 Doctrine 1.2,所以无法对此进行测试。但我认为这个想法很明确:)

【讨论】:

谢谢。我几乎用过这样的东西——希望有一个很酷的衬里:)

以上是关于PHP Doctrine 1.2 SET 语句中的条件 IF 语句的主要内容,如果未能解决你的问题,请参考以下文章