0

次のクエリを実行する必要があります(Propel 1.4 / Symfony 1.4を使用)

update notification
set read=1
where to=6 AND
    action=0

このために、私はsymfony1.4で次のコードを書きました

$c=new Criteria()
$c->add(NotificationPeer::TO, $memberId, Criteria::EQUAL);
$c->add(NotificationPeer::ACTION, 0, Criteria::EQUAL);
$notification = NotificationPeer::doSelect($c);

foreach($notification as $notice)
{
    $notice->setRead(1);
    $notice->save();
}

動作しますが、ユーザーに何百もの通知がある場合、何百ものクエリとサーバーへの不要な負荷がかかります。私は推進のdoUpdateメソッドを調べました、私はそれが私のために働くことができると思いますが、どのように理解することができません。

単一のクエリですべてのことを行う方法はありますか(あることは知っていますが、わかりません)?

4

1 に答える 1

3

2つの基準を作成する必要があります。

  • where句用に1つ
  • もう1つはupdate句用です。
// Build WHERE criteria
$wherec = new Criteria();
$wherec->add(NotificationPeer::TO, $memberId, Criteria::EQUAL);
$wherec->add(NotificationPeer::ACTION, 0, Criteria::EQUAL);

// Build updated field criteria
$updc = new Criteria();
$updc->add(NotificationPeer::READ, 1);

BasePeer::doUpdate($wherec, $updc, $con);

1つの(大きくなる可能性がある)クエリを実行します。このスニペットを参照してください。

于 2012-09-05T14:05:56.203 に答える