This article to give your classmates about Yii deletebyattributs usage, cautious use of DAO Delete, I hope this article will be helpful to everyone.
Yii framework must be careful to use the DAO delete, accidentally it can not be conditions, it becomes the whole table delete.
Can be used ActiveRecord deletebyattributes or DeleteAll method is relatively not easy to write wrong.
Deletebyattributes usage is as follows:
| The code is as follows |
Copy Code |
Myclass::model ()->deleteallbyattributes (Array ( ' Phone_number ' = $phoneNumber, )); |
Or the first argument is empty, use the second condition argument
| The code is as follows |
Copy Code |
Myclass::model ()->deleteallbyattributes (Array (), ' phone_number ' =:p hone_number ', Array ( ':p hone_number ' = $phoneNumber, )); |
or use DeleteAll ():
| The code is as follows |
Copy Code |
Myclass::model ()->deleteall (' phone_number ' =:p hone_number ', Array ( ':p hone_number ' = $phoneNumber, )); |
Another one with an in condition.
| |
copy code |
| $condition = new Cdbcriteria (); $condition->addcondition (' status=:status '); $condition->params = Array (': Status ' =>1); $condition->addincondition (' user_id ', Array (100111,100221,100221)); User::model ()->deleteall ($condition);D An example of an AO band in condition Yii::app ()->db->createcommand () ->delete (' Mw_user ', Array (' and ', ' user_id=:user_id ', Array (' In ', ' position_id ', array (+/-)), Array (': user_id ' =>121111)); |
However, please use the DAO's delete carefully, when your condition is wrong, it will not be able to generate a where condition, while the SQL statement also does not have where, but not necessarily error, the result is no where delete, the result is the whole table is deleted.
http://www.bkjia.com/PHPjc/633167.html www.bkjia.com true http://www.bkjia.com/PHPjc/633167.html techarticle This article to give your classmates about Yii deletebyattributs usage, cautious use of DAO Delete, I hope this article will be helpful to everyone. Yii framework must be cautious with the DAO Delete, accidentally ...