Yii:deleteAll和IN语句

时间:2013-05-28 17:36:15

标签: yii

我有带有ID的数组,想要通过id

删除它们
$membersId = array('1','2','3');
$inStatement = implode(',', $membersId);

//dont work!
TRatingMembers::model()->deleteAll(array('member_id IN (:member_id)'),
    array(':member_id' => $inStatement));

如何使用一个事务删除所有行?

2 个答案:

答案 0 :(得分:19)

$criteria = new CDbCriteria;
$criteria->addInCondition('member_id',$membersId);
TRatingMembers::model()->deleteAll($criteria);

http://www.yiiframework.com/doc/api/1.1/CDbCriteria#addInCondition-detail

答案 1 :(得分:4)

if (!empty(membersId)) {
    $memberIds = implode(', ', $membersId);
    TRatingMembers::model()->deleteAll('member_id IN (' . $memberIds . ')');
}