如果我有一系列主键,然后是另一个键值对标题:
$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')
);
我正在寻找一种方法来在1 Propel调用中更新我的文章表。
答案 0 :(得分:2)
我认为你不能。如果您考虑如何在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);