Database Operations of yii

Source: Internet
Author: User

1,

$admin=Admin::model()->findAll($condition,$params);     

 
This method queries a set based on a condition, such:

findAll("username=:name",array(":name"=>$username));     

 
 
2,

$admin=Admin::model()->findAllByPk($postIDs,$condition,$params); findAllByPk($id,"name like ':name' and age=:age" ,array(':name'=>$name,'age'=>$age));     

 
This method queries a set based on the primary key. Multiple Primary keys can be used, for example:
Findallbypk (Array (1, 2 ));

 
3,

$admin=Admin::model()->findAllByAttributes($attributes,$condition,$params); 

 
This method is used to query a set based on conditions. Multiple conditions can be used to put conditions in an array, for example:
Findallbyattributes (Array ('username' => 'admin '));
 
 
4,

$admin=Admin::model()->findAllBySql($sql,$params); 

 
This method queries an array based on SQL statements, such:
Findallbysql ("select * from Admin where username =: Name", array (': name' => 'admin '));
-----------------------------------------------------------------------------
Ii. Method for querying objects
1,

$admin=Admin::model()->findByPk($postID,$condition,$params); 

 
Query an object based on the primary key, for example, findbypk (1 );
 
2,

$row=Admin::model()->find($condition,$params); 

 
A group of data may be queried based on one condition, but only the first row of data is returned, for example:
Find ('username =: name', array (': name' => 'admin '));
 
3,

$admin=Admin::model()->findByAttributes($attributes,$condition,$params); 

 
This method is used to query a group of data based on conditions. Multiple conditions can be used to put conditions into the array. the first data queried by the condition is also the first data, for example:
Findbyattributes (Array ('username' => 'admin '));
 
 
4,

$admin=Admin::model()->findBySql($sql,$params); 

 
This method is used to query a group of data based on an SQL statement. It also queries the first data, such:
Findbysql ("select * from Admin where username =: Name", array (': name' => 'admin '));
 
5. spell out a method to obtain SQL, and query an object based on find.

$criteria=new CDbCriteria; $criteria->select='username';  // only select the 'title' column $criteria->condition='username=:username'; $criteria->params=array(':username=>'admin'); $post=Post::model()->find($criteria); // $params is not needed 

 
 
------------------------------------------------------------------------------
Iii. number of queries to determine whether the query has results
1,

$n=Post::model()->count($condition,$params); 

 
This method queries the number of records in a set based on a condition and returns an int number, as shown in figure
Count ("username =: Name", array (": Name" => $ username ));
 
2,

$n=Post::model()->countBySql($sql,$params); 

 
This method queries the number of records in a set based on SQL statements and returns an int number, as shown in figure
Countbysql ("select * from Admin where username =: Name", array (': name' => 'admin '));
 
3,

$exists=Post::model()->exists($condition,$params); 

 
This method is used to query the queried Array Based on a condition. If any data exists, a true value is returned. Otherwise, no data is found.
 
 
========================================================== ========================================================== ======================================
Iv. Add Methods

$ Admin = new admin; $ admin-> username = $ username; $ admin-> Password = $ password; if ($ admin-> Save ()> 0) {echo "added successfully";} else {echo "failed to add ";}

========================================================== ========================================================== ==========================================
5. Modification Method

1. Post: Model ()-> updateall ($ attributes, $ condition, $ Params); $ COUNT = admin: Model () -> updateall (Array ('username' => '000000', 'Password' => '000000'), 'password =: pass', array (': pass '=> '1111a1'); If ($ count> 0) {echo "modified successfully";} else {echo "failed to modify ";}

 
2,

Post: Model ()-> updatebypk ($ PK, $ attributes, $ condition, $ Params); $ COUNT = admin: Model ()-> updatebypk (1, array ('username' => 'admin', 'Password' => 'admin'); $ COUNT = admin: Model ()-> updatebypk (Array (1, 2 ), array ('username' => 'admin', 'Password' => 'admin'), 'username =: name', array (': name' => 'admin'); If ($ count> 0) {echo "modification succeeded";} else {echo "modification failed ";}

$ PK indicates the primary key, either a set or a set, $ attributes indicates the set of fields to be modified, $ condition indicates the condition, and $ Params indicates the input value.
 
3,

Post: Model ()-> updatecounters ($ counters, $ condition, $ Params); $ COUNT = admin: Model () -> updatecounters (Array ('status' => 1), 'username =: name', array (': name' => 'admin ')); if ($ count> 0) {echo "modification succeeded";} else {echo "modification failed ";}

Array ('status' => 1) indicates the admin table in the database. According to the condition username = 'admin', The Status field of all the queried results is incremented by 1.
 
========================================================== ========================================================== ========================================================== ======================================
Vi. Deletion Method
1,

Post: Model ()-> deleteall ($ condition, $ Params); $ COUNT = admin: Model ()-> deleteall ('username =: name and password =: pass ', array (': name' => 'admin', ': pass' => 'admin'); $ id = 1, 2, 3 deleteall ('Id in (". $ id. ") '); if ($ count> 0) {echo" deleted successfully ";} else {echo" failed to delete ";}

 
2,

Post: Model ()-> deletebypk ($ PK, $ condition, $ Params); $ COUNT = admin: Model ()-> deletebypk (1 ); $ COUNT = admin: Model ()-> deletebypk (Array (1, 2), 'username =: name', array (': name' => 'admin ')); if ($ count> 0) {echo "deleted successfully";} else {echo "failed to delete ";}

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.