Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii: deleteAll and IN statement

Tags:

yii

I have array with ids and want to delete all them by id

$membersId = array('1','2','3');
$inStatement = implode(',', $membersId);

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

How to delete all rows with one transaction?

like image 503
Oleg Beat Avatar asked May 28 '13 17:05

Oleg Beat


2 Answers

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

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

like image 59
jborch Avatar answered Oct 05 '22 18:10

jborch


if (!empty(membersId)) {
    $memberIds = implode(', ', $membersId);
    TRatingMembers::model()->deleteAll('member_id IN (' . $memberIds . ')');
}
like image 24
Valery Viktorovsky Avatar answered Oct 05 '22 20:10

Valery Viktorovsky