YiideleteByAttributs usage, use Dao's delete with caution

Source: Internet
Author: User
This article will introduce the usage of YiideleteByAttributs 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... 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:

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

Or the first parameter is null and the second condition parameter is used.

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

Or use deleteAll ():

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

Let's add another one with the in condition.

$ 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 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 ));

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.


Address:

Reprinted at will, but please attach the article address :-)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.