我不认为你可以。如果您考虑如何在 MySQL 中执行此类操作,则不会在单个查询中进行。它们必须是单独的语句,例如
UPDATE `article` SET title = "New Title" WHERE id = 1;
UPDATE `article` SET title = "New Title2" WHERE id = 2;
您可能已经可以进行符合指定条件的批量更新,如下所示:
// set the select condition criteria
$c = new Criteria();
$c->add(ArticlePeer::ID, 1);
// set the update criteria
$update = new Criteria();
$update->add(ArticlePeer::TITLE, 'New Title');
// we need the connection for update, so get default connection
$con = Propel::getConnection();
// finally, do the update
BasePeer::doUpdate($c, $update, $con);
但这对您的实例没有多大帮助,因为选择标准条件在您的每个更新实例中都会发生变化。也许你可以将上面的代码包含在一个循环遍历你的数组的 for 循环中。
更新:
您可以尝试如下的 Propel hack(未经测试):
$article = array(
array('Id' => 1, 'Title' => 'New Title'),
array('Id' => 2, 'Title' => 'New Title2'),
array('Id' => 3, 'Title' => 'New Title3'),
array('Id' => 4, 'Title' => 'New Title4')
);
$ids = array();
$when = 'CASE id';
foreach ($article as $a) {
$ids[] = $a['Id'];
$when .= ' WHEN ' . $a['Id'] . ' THEN ' . $a['Title'];
}
$when .= ' END';
$c = new Criteria();
$c->add(ArticlePeer::ID, $ids, Criteria::IN);
$update = new Criteria();
$update->add(ArticlePeer::TITLE, $when);
$con = Propel::getConnection();
BasePeer::doUpdate($c, $update, $con);