This article will introduce you to Yii deleteByAttributs usage and use Dao's delete with caution. I hope this article will be helpful to you.
The Yii framework must use the delete Statement of Dao with caution. If it fails to generate a condition, the entire table is deleted.
You can use the deleteByAttributes or deleteAll method of ActiveRecord to write errors.
DeleteByAttributes usage:
| The Code is as follows: |
Copy code |
|
MyClass: model ()-> deleteAllByAttributes (array (
'Phone _ number' => $ phoneNumber,
)); |
Or the first parameter is null and the second condition parameter is used.
| The Code is as follows: |
Copy code |
|
MyClass: model ()-> deleteAllByAttributes (array (), ''phone _ number' =: phone_number ', array (
': Phone_number' => $ phoneNumber,
)); |
Or use deleteAll ():
| The Code is as follows: |
Copy code |
|
MyClass: model ()-> deleteAll (''phone _ number' =: phone_number ', array (
': Phone_number' => $ phoneNumber,
)); |
Let's add another one with the in condition.
| The Code is as follows: |
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); example of Dao with in condition
Yii: app ()-> db-> createCommand ()
-> Delete ('MW _ user', array ('and', 'user _ id =: user_id ', array ('in', 'position _ id ', array (121111, 3), array (': user_id' => ));
|
However, use the delete Statement of DAO with caution. When you write an error in the condition, it cannot generate the where condition, and the SQL statement does not have the where condition, but it does not necessarily report an error, the result is a delete without where, and the result is that the entire table is deleted.