The novice yii, organized a number of YII database related operations, common learning, common progress.
One, query data collection
1 //1. The method is to query a set based on a condition2 $admin=admin::model ()->findall ($condition,$params);3 $admin=admin::model ()->findall ("Username=:name",Array(": name" + =$username));4 5 //2. FINDALLBYPK (the method is to query a collection based on the primary key, you can use multiple primary keys)6 $admin=admin::model ()->FINDALLBYPK ($postIDs,$condition,$params);7 $admin=admin::model ()->FINDALLBYPK ($id, "Name Like:name and Age=:age",Array(': name ' = =$name, ' age ' =$age));8 $admin=admin::model ()->FINDALLBYPK (Array());9 Ten //3.findAllByAttributes (the method is to query a set based on criteria, can be multiple conditions, put the conditions in the array) One $admin=admin::model ()->findallbyattributes ($attributes,$condition,$params); A $admin=admin::model ()->findallbyattributes (Array(' username ' = ' admin ')); - - //4.FINDALLBYSQL (This method queries an array based on the SQL statement) the $admin=admin::model ()->findallbysql ($sql,$params); - $admin=admin::model ()->findallbysql ("select * from Admin where username Like:name",Array(': Name ' = '%ad% '));
Ii. object of Inquiry
1 //An object is queried based on the primary key, such as: FINDBYPK (1);2 $admin=admin::model ()->FINDBYPK ($postID,$condition,$params); 3 $admin=admin::model ()->FINDBYPK (1);4 5 //query a set of data based on one condition, possibly multiple, but he only returns the first row of data6 $row=admin::model ()->find ($condition,$params);7 $row=admin::model ()->find (' Username=:name ',Array(': Name ' = ' admin '));8 9 //the method is to query a set of data according to the condition, can be more than one condition, put the condition into the array, query is also the first dataTen $admin=admin::model ()->findbyattributes ($attributes,$condition,$params); One $admin=admin::model ()->findbyattributes (Array(' username ' = ' admin ')); A - //the method is to query a set of data according to the SQL statement, he queried the first data - $admin=admin::model ()->findbysql ($sql,$params); the $admin=admin::model ()->findbysql ("select * from Admin where Username=:name",Array(': Name ' = ' admin ')); - - //To make a method of getting SQL, to query an object based on find - $criteria=NewCdbcriteria; + $criteria->select= ' username ';//Only Select the ' title ' column - $criteria->condition= ' Username=:username '; + $criteria->params=Array(":username=> ' admin '"); A $criteria->order = "id DESC"; at $criteria->limit = "3"; - $post=post::model ()->find ($criteria);//$params IsNot needed
Third, the number of records to meet the conditions of the query
1 //the method is to query the number of records of a collection based on a condition, returning an int type number2 $n=post::model ()Count($condition,$params); 3 $n=post::model ()Count("Username=:name",Array(": name" + =$username));4 5 //the method is to query the number of records in a collection based on the SQL statement, returning an int type number6 $n=post::model ()->countbysql ($sql,$params);7 $n=post::model ()->countbysql ("select * from admin where Username=:name",Array(': Name ' = ' admin '));8 9 //The method is based on a conditional query query to get the array with no data, if there is data returned by a true, otherwise not foundTen $exists=post::model ()->exists ($condition,$params); One $exists=post::model ()->exists ("Name=:name",Array(": name" + =$username));
Iv. add
1 $admin New Admin; 2 $admin $username 3$admin$password4if($admin Echo "Add success"; }Elseecho "Add failed"; }
V. Modification
1Post::model ()->updateall ($attributes,$condition,$params); 2 $count=admin::model ()->updateall (Array(' username ' = ' 11111 ', ' password ' = ' 11111 '), ' password=:p ',Array(':p ' and ' 1111a1 ')); 3 if($count> 0) {Echo"Successful modification"; }Else{Echo"Modify Failed"; }4 5 //$PK primary key, which can be either a collection, $attributes is a collection of fields to modify, $condition criteria, $params incoming values6Post::model ()->UPDATEBYPK ($PK,$attributes,$condition,$params); 7 $count=admin::model ()->UPDATEBYPK (1,Array(' username ' = ' admin ', ' password ' = ' admin ')); 8 $count=admin::model ()->UPDATEBYPK (Array(),Array(' username ' = ' admin ', ' password ' = ' admin '), ' username=:name ',Array(': Name ' = ' admin ')); 9 if($count>0) {Echo"Successful modification"; }Else{Echo"Modify Failed"; }Ten OnePost::model ()->updatecounters ($counters,$condition,$params); A $count=admin::model ()->updatecounters (Array(' status ' =>1), ' Username=:name ',Array(': Name ' = ' admin ')); - if($count> 0) {Echo"Successful modification"; }Else{Echo"Modify Failed"; } - //The array (' status ' =>1) represents the admin table in the database according to the condition username= ' admin ', and all the result status fields of the query are added 1
Vi. deletion of
1 //DeleteAll2Post::model ()->deleteall ($condition,$params); 3 $count= Admin::model ()->deleteall (' Username=:name and password=:pArray(': Name ' = ' admin ', ':p ', ' + ' admin ')); 4 $count= Admin::model ()->deleteall (' ID in (") ');//Delete data with ID for these5 if($count>0) {Echo"Delete succeeded"; }Else{Echo"Delete Failed"; }6 7 //DELETEBYPK8Post::model ()->DELETEBYPK ($PK,$condition,$params); 9 $count= Admin::model ()->deletebypk (1); Ten $count=admin::model ()->DELETEBYPK (Array, ' Username=:name ',Array(': Name ' = ' admin ')); One if($count>0) {Echo"Delete succeeded"; }Else{Echo"Delete Failed"; }