Home  >  Article  >  Backend Development  >  Yii deleteByAttributs usage, use Dao's delete_PHP tutorial with caution

Yii deleteByAttributs usage, use Dao's delete_PHP tutorial with caution

WBOY
WBOYOriginal
2016-07-13 10:43:19972browse

This article will introduce to you the usage of Yii deleteByAttributs. Use Dao's delete with caution. I hope this article will be helpful to everyone.

Yii framework must use Dao's delete with caution. If it accidentally fails to generate the conditions, it will become a complete table deletion.

You can use ActiveRecord’s deleteByAttributes or deleteAll methods which are relatively easy to write incorrectly.

The usage of deleteByAttributes is as follows:

The code is as follows
 代码如下 复制代码

MyClass::model()->deleteAllByAttributes(array(
    'phone_number'=>$phoneNumber,
));

Copy code

 代码如下 复制代码

MyClass::model()->deleteAllByAttributes(array(),'`phone_number` = :phone_number',array(
    ':phone_number'=>$phoneNumber,
));

MyClass::model()->deleteAllByAttributes(array(
'phone_number'=>$phoneNumber,
));

 代码如下 复制代码

MyClass::model()->deleteAll('`phone_number` = :phone_number',array(
    ':phone_number'=>$phoneNumber,
));

or if the first parameter is empty, use the second conditional parameter
 代码如下 复制代码

$condition = new CDbCriteria();
$condition->addCondition('status=:status');
$condition->params = array(':status'=>1);
$condition->addInCondition('user_id',array(100111,100221,100221));
User::model()->deleteAll($condition);Dao带in条件的示例


Yii::app()->db->createCommand()
->delete('mw_user', array('and', 'user_id=:user_id', array('in', 'position_id', array(1,2,3))),array(':user_id'=>121111));

MyClass::model()->deleteAllByAttributes(array(),'`phone_number` = :phone_number',array(
':phone_number'=>$phoneNumber,
));
The code is as follows

Copy code

The code is as follows Copy code
MyClass::model()->deleteAll('`phone_number` = :phone_number',array(
':phone_number'=>$phoneNumber,
)); Another one with 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);Dao example with in condition

Yii::app()->db->createCommand()
->delete('mw_user', array('and', 'user_id=:user_id', array('in', 'position_id', array(1,2,3))),array(':user_id'= >121111)); But please use DAO's delete with caution. If you write the conditions wrong, it will not be able to generate the where condition. At the same time, there is no where in the sql statement, but it does not necessarily report an error. The result will be delete without where. The result will be that the entire table is deleted. http://www.bkjia.com/PHPjc/633167.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633167.htmlTechArticleThis article will introduce to you the usage of Yii deleteByAttributs. Use Dao delete with caution. I hope this article will be helpful to everyone. Helps. The Yii framework must use Dao's delete with caution. If you are not careful...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn