This paper describes the model query techniques of YII based on arrays and objects. Share to everyone for your reference, as follows:
For a model Post like the next 4 Query method, return an object or an array of objects.
Finds the first line in the results that meet the specified criteria find the satisfying, the specified Condition$post=post::model ()->find ($condition, $ params);//Find the row with the specified primary key value find the row with the specified primary Key$post=post::model ()->findbypk ($postID, $condition , $params);//Find the row with the specified attribute value find the row with the specified attribute Values$post=post::model ()->findbyattributes ($ Attributes, $condition, $params);//Not found returns null//the first row in the results by specifying the SQL statement find the the the primary line using the specified SQL statement$ Post=post::model ()->findbysql ($sql, $params);
If the Find method finds a row that satisfies the query criteria, it returns a post instance whose properties contain the values of the corresponding columns in the row of the data table. Then we can read the loaded values like the properties of a normal object, such as Echo $post->title;. If you do not find anything in the database using the given query criteria, the Find method returns NULL.
When we call find, we use $condition and $params to specify the query criteria. Here $condition can be a where string in an SQL statement, $params a parameter array where the value should be bound to a placeholder in $condation. For example, suppose we query for PostID = 10 data
Find the row with Postid=10$post=post::model ()->find (' postid=:p ostid ', Array (':p ostid ' =>10));
Condition $condition is the where part of our SQL, what to do with the parameter, pass through the params, but the name is added ":".
Yii has a Cdbcriteria class to construct the query, so if we query the Title,cdbcriteria of PostID 10
$criteria =new Cdbcriteria; $criteria->select= ' title '; Only select the ' title ' column$criteria->condition= ' postid=:p ostid '; $criteria->params=array (':p ostid ' = $post =post::model ()->find ($criteria); $params is not needed
One way to replace Cdbcriteria is to pass an array to the Find method. The key and value of the array correspond to the property name and value of the standard (criterion), and the above example can be rewritten as follows:
$post =post::model ()->find (Array ( ' select ' = ' title ', ' condition ' = ' postid=:p ostid ', ' Params ' =>array (':p ostid ' =>10),));
Of course also applies to findall ()
self::$_items[$type]=array (), $models =self::model ()->findall (Array ( ' condition ' = ' type=:type ', ' Params ' =>array (': type ' = ' = $type), ' order ' = ' position ',));
When a query condition is about matching several columns by a specified value, we can use Findbyattributes (). We make the $attributes parameter an array of values indexed by the column name.
The $attributes in Findbyattributes is the name of the field. Query title is ABC how to query it? See below
Copy the Code code as follows: Post::model ()->findbyattributes (' title ' = ' abc ')
Other methods:
1, $admin =admin::model ()->findall ($condition, $params);
The method is to query a collection based on a condition, such as:
Copy the Code code as follows: 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));
The method is to query a collection based on the primary key, which can use multiple primary keys, such as:
Copy the Code code as follows: FINDALLBYPK (array);
3, $admin =admin::model ()->findallbyattributes ($attributes, $condition, $params);
The method is to query a set based on the condition, can be multiple conditions, put the conditions in the array, such as:
Copy the code as follows: Findallbyattributes (Array (' username ' = ' admin '));
4, $admin =admin::model ()->findallbysql ($sql, $params);
The method is to query an array based on an SQL statement, such as:
Copy the Code code as follows: Findallbysql ("select *from admin where Username=:name", Array (': Name ' = ' admin '));
Second, query the method of the image
1, $admin =admin::model ()->findbypk ($postID, $condition, $params);
According to the primary key query an object, such as: copy code code as follows: FINDBYPK (1);
2, $row =admin::model ()->find ($condition, $params);
A set of data is queried based on one condition, possibly multiple, but he returns only the first row of data, such as:
Copy the code as follows: Find (' Username=:name ', Array (': Name ' = ' admin '));
3, $admin =admin::model ()->findbyattributes ($attributes, $condition, $params);
The method is to query a set of data according to criteria, can be multiple conditions, put the conditions in the array, he queried the first data, such as:
Copy the code as follows: Findbyattributes (Array (' username ' = ' admin '));
4, $admin =admin::model ()->findbysql ($sql, $params);
The method is to query a set of data based on the SQL statement, and he queries the first data, such as:
Copy the Code code as follows: Findbysql ("select *from admin where Username=:name", Array (': Name ' = ' admin '));
5, spell a method of obtaining SQL, in accordance with the find query out an object
$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
Third, the number of queries to determine whether the query has results
1, $n =post::model ()->count ($condition, $params);
The method is to query the number of records for a collection based on a condition, and return an int type, such as
Copy the code as follows: Count ("Username=:name", Array (": Name" = $username));
2, $n =post::model ()->countbysql ($sql, $params);
The method is to query the number of records in a collection based on the SQL statement, returning an int type number, such as
Copy the Code code as follows: Countbysql ("select *from admin where Username=:name", Array (': Name ' = ' admin '));
3, $exists =post::model ()->exists ($condition, $params);
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 found
Iv. methods to be added
$admin =new Admin, $admin->username= $username, $admin->password= $password, if ($admin->save () >0) { echo "Add Success";} else{ echo "Add Failed";}
V. Methods of Modification
1, Post::model ()->updateall ($attributes, $condition, $params);
$count = Admin::model ()->updateall (Array (' username ' = ' 11111 ', ' password ' = ' 11111 '), ' password=:p Array (':p to ' 1111a1 ')), if ($count >0) { echo "modified successfully";} else{ echo "modification failed";}
2, Post::model ()->updatebypk ($PK, $attributes, $condition, $params);
$count = Admin::model ()->updatebypk (1,array (' username ' = ' admin ', ' password ' = ' admin ')); $count = admin:: Model ()->UPDATEBYPK (array), Array (' username ' = ' admin ', ' password ' = ' admin '), ' Username=:name ', Array ( ': Name ' = ' admin '); if ($count >0) { echo "modified successfully";} else{ echo "modification failed";}
$PK represents a primary key, which can be either a collection, $attributes represents a collection of fields to modify, $condition represents a condition, $params an incoming value
3, Post::model ()->updatecounters ($counters, $condition, $params);
$count =admin::model ()->updatecounters (Array (' status ' =>1), ' Username=:name ', Array (': Name ' = ' Admin ')); ($count >0) { echo "modified successfully";} else{ echo "modification 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. Methods of deletion
1, Post::model ()->deleteall ($condition, $params);
$count = Admin::model ()->deleteall (' Username=:name and password=:p ", Array (': Name ' = ' Admin ', ':p ' Admin ')); $id =1,2,3 deleteAll (' ID in ('. $id. ') '); Delete the data ID for these if ($count >0) { echo "delete succeeded";} else{ echo "Delete failed";}
2, Post::model ()->deletebypk ($PK, $condition, $params);
$count = Admin::model ()->deletebypk (1), $count = Admin::model ()->deletebypk (array), ' Username=:name ', array (': Name ' = ' admin ')); if ($count >0) { echo "delete succeeded";} else{ echo "Delete failed";}
It is hoped that this article is helpful to the PHP program design based on YII framework.