Summary of common database operations implemented by the Yii2 framework, and the yii2 framework

Source: Internet
Author: User
Tags findone

Summary of common database operations implemented by the Yii2 framework, and the yii2 framework

General:

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

Query:

Query:

$rows = (new \yii\db\Query())      ->select(['code', 'name', 'population'])      ->from('country')      ->limit(10)      ->all();

Select:

$ Data = $ query-> select (['code', 'name'])-> from ('country')-> all (); // call the yii \ db \ Query: addSelect () method to select the additional field $ 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'

Hash format, for example: ['status' => 1, 'type' => 2]

Operator format, for example, ['like', 'name', 'test']

andFilterWhere()orFilterWhere()

Active Record (AR) provides an object-oriented interface to access data in the database. An AR class associates a data table. Each AR object corresponds to a row in the table. The attributes of the object (that is, the Attribute of the AR) are mapped to the corresponding columns of the Data row. An active record (AR object) corresponds to a row of the data table, and the attributes of the AR object map to the corresponding columns of the row.

The addition, deletion, and modification operations use AR objects for ing.

Add

$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();

Others

User: find ()-> all (); // return all User data; User: findOne ($ id); // return a piece of data with primary key id = 1; user: find ()-> where (['name' => 'ttt'])-> one (); // return a piece of data from ['name' => 'ttt']; User: find ()-> where (['name' => 'ttt']) -> all (); // return all data of ['name' => 'ttt']; User: findBySql ('select * FROM user ') -> all (); // use an SQL statement to query all data in the user table. User: findBySql ('select * FROM user')-> one (); // This method uses an SQL statement to query a piece of data in the user table. User: find ()-> andWhere (['sex' => 'femal ', 'age' => '18'])-> count ('id'); // calculates the total number of entries that meet the condition. User: find ()-> one (); // return a piece of data; User: find ()-> all (); // return all data; User: find ()-> count (); // number of returned records; User: find ()-> average (); // returns the average value of the specified column; User: find ()-> min (); // return the minimum value of the specified column; User: find ()-> max (); // return the maximum value of the specified column; User: find ()-> scalar (); // query result of the first column of the first row of the returned value; User: find ()-> column (); // return the value of the first column in the query result; User :: find ()-> exists (); // return a value indicating whether the data row contains the query result;

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.