Summary of common operation of database for YII2 framework

Source: Internet
Author: User
Tags findone yii
This article mainly introduces the YII2 framework implementation of the database common operation summary, small series feel very good, and now share to everyone, but also for everyone to do a reference. Let's take a look at it with a little knitting.

General:

Use yii\db\query; $query = new Query ();

Inquire:

Query:

$rows = (new \yii\db\query ())      ->select ([' Code ', ' name ', ' population '])      ->from (' Country ')      Limit (Ten)      ->all ();

Select:

$data = $query->select ([' Code ', ' name '])->from (' Country ')->all ();//Call the Yii\db\query::addselect () method to select additional fields    $data = $query->select ([' Code ', ' name '])      ->addselect ([' population '])->all ();

From:

$query->from (' country '); $query->from ([' Public.country C ']); $query->from (' public.country c ');

Where:

string format, for example: ' Status=1 '

Hashed format, for example: [' status ' = = 1, ' type ' and ' = 2]

operator format, for example: [' like ', ' name ', ' Test ']

Andfilterwhere () Orfilterwhere ()

The active record (activity log, AR) provides an object-oriented interface to access data in the database. An AR class is associated with a data table, each AR object corresponds to a row in the table, and the properties of the object (that is, the AR attribute attribute) map to the corresponding column of the data row. An Activity record (AR object) corresponds to a row of the data table, and the properties of the AR object map the corresponding column of the row.

These additions and deletions will be used to map the AR object.

Increase

$country->name = ' UK '; $country->save ();

Modify

$country = Customer::findone ($id); $country->email = ' UK '; $country->save (); Equivalent to $country->update ();

Delete

$country = Country::findone ($id); $country->delete ();

Other

User::find ()->all ();  return all user data; User::findone ($id);  Returns a data for the primary key id=1; User::find ()->where ([' name ' = ' TTT '])->one ();  Returns a piece of data for [' name ' = ' TTT '], User::find ()->where ([' name ' = ' TTT '])->all ();  Returns all data for [' name ' = ' TTT '], user::findbysql (' SELECT * from User ')->all (); Querying all data in the user table with SQL statements; User::findbysql (' SELECT * from user ')->one (); This method uses the SQL statement to query a data in the user table, User::find ()->andwhere ([' sex ' = ' female ', ' age ' = '] ')->count (' id ');  Statistics of the total number of bars that meet the conditions; User::find ()->one ();  Returns a piece of data; User::find ()->all ();  return all data; User::find ()->count ();  Returns the number of records; User::find ()->average ();  Returns the average of the specified column; User::find ()->min ();  Returns the minimum value of the specified column; User::find ()->max ();  Returns the maximum value of the specified column; User::find ()->scalar ();  Returns the query result for the first column of the first row of the value; User::find ()->column ();  Returns the value of the first column in the query result; User::find ()->exists ();  Returns a value indicating whether the data row contains the query result;

The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we support topic.alibabacloud.com.

Related Article

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.