Yii example of model query technique based on array and object _php example

Source: Internet
Author: User
Tags first row yii

This article illustrates Yii's model query techniques based on arrays and objects. Share to everyone for your reference, specific as follows:

For a model Post like the following 4 Query method, return an object or an array of objects.

Finds the first row in a result that satisfies a specified condition find the the satisfying the specified condition
$post =post::model ()->find ($condition, $ params);
Finds the row with the specified primary key value find the row with the specified primary key
$post =post::model ()->findbypk ($postID, $condition, $ params);
Finds the row with the specified property value specified attribute values
$post =post::model ()->findbyattributes ($ Attributes, $condition, $params);//no return null//found the
first row in the result with the specified SQL statement find the "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 value of the corresponding column in the datasheet row. We can then read the loaded values like the properties of normal objects, such as Echo $post->title;. If nothing is found 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 An array of arguments, where the value should be bound to a placeholder in $condation. For example: Suppose we query the data for PostID = 10

Find the row with postid=10
$post =post::model ()->find (' postid=:p ostid ', Array (':p ostid ' =>10));

The conditional $condition is the where part of our SQL, what about the parameters, passing through the params, but the name is added ":".

Yii has a Cdbcriteria class to construct the query, if we query PostID 10 Title,cdbcriteria is constructed like this

$criteria =new Cdbcriteria;
$criteria->select= ' title '; Only select the ' title ' column
$criteria->condition= ' postid=:p ostid ';
$criteria->params=array (':p ostid ' =>10);
$post =post::model ()->find ($criteria); $params is not needed

One way to replace Cdbcriteria is to pass an array to the Find method. The keys and values of the array correspond to the attribute names and values of the standard (criterion), and the example above 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 for ABC how to inquire? See below

Copy Code code as follows:
Post::model ()->findbyattributes (Array (' title ' => ' abc '))

Other methods:

1, $admin =admin::model ()->findall ($condition, $params);

The method is to query a set based on a condition, such as:

Copy 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, and you can use multiple primary keys, such as:
Copy Code code as follows:
FINDALLBYPK (Array (1,2));

3, $admin =admin::model ()->findallbyattributes ($attributes, $condition, $params);

The method is to query a set according to the condition, can be multiple conditions, put the condition into the array, such as:

Copy Code 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 Code code as follows:
Findallbysql ("Select *from admin where Username=:name", Array (': Name ' => ' admin '));

Second, the method of querying the image

1, $admin =admin::model ()->findbypk ($postID, $condition, $params);

Query out an object based on the primary key, such as:

Copy Code code as follows:
FINDBYPK (1);

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

Query out a set of data based on a condition, possibly multiple, but he returns only the first row of data, such as:

Copy Code 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 based on conditions, can be multiple conditions, put the conditions in the array, he queried the first data, such as:

Copy Code 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 Code code as follows:
Findbysql ("Select *from admin where Username=:name", Array (': Name ' => ' admin '));

5, spell a way to get the SQL, in accordance with 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 inquiries to determine whether the query has results

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

The method is based on a condition that queries how many records a collection has, and returns an int number, as

Copy Code 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 number, as

Copy 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 condition that queries the query to get an array of no data, if any data returns a true, otherwise no

Iv. Methods of Addition

$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 ' => ' 1111a1 '));
if ($count >0) {
  echo "modified successfully";
} else{
  echo "Modify failed";
}

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 "modified successfully";
} else{
  echo "Modify failed";
}

A $PK represents a primary key, can be one or a collection, $attributes represents a collection of fields to be modified, $condition represents a condition, $params an incoming value

3, Post::model ()->updatecounters ($counters, $condition, $params);

$count =admin::model ()->updatecounters (' status ' =>1), ' Username=:name ', Array (': Name ' => ' Admin '));
if ($count >0) {
  echo "modified successfully";
} else{
  echo "Modify failed";
}

Array (' status ' =>1) represents the admin table in the database according to the condition username= ' admin ', all the results in the Status field 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 with IDs of 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 (1,2), ' Username=:name ', Array (': Name ' => ' Admin '));
if ($count >0) {
  echo "delete succeeded";
} else{
  echo "Delete failed";
}

I hope this article will help you with the PHP program design based on the YII framework.

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.