SoFunction
Updated on 2025-03-09

Summary of common operations of Yii2 framework implementing database

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 additional fields    $data = $query->select(['code', 'name'])
      ->addSelect(['population'])->all();

From:

$query->from('country'); 
$query->from([' c']); 
$query->from(' 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  (Activity Record, 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 object's attributes (that is, the attributes of AR) are mapped to the corresponding columns of the data row. An active record (AR object) corresponds to a row in the data table, and the properties of the AR object map the corresponding column of the row.

The addition, deletion and modification here will use AR objects for mapping operations.

Increase

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

Revise

$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);  //Return a piece of data with primary key id=1;User::find()->where(['name' => 'ttt'])->one();  //Return a piece of data of ['name' => 'ttt'];User::find()->where(['name' => 'ttt'])->all();  //Return all data of ['name' => 'ttt'];User::findBySql('SELECT * FROM user')->all(); // Use sql statement to query all data in the user table;User::findBySql('SELECT * FROM user')->one(); //This method is to query a piece of data in the user table using sql statement;User::find()->andWhere(['sex' => 'female', 'age' => '18'])->count('id');  //Calculate the total number of items that meet the conditions;User::find()->one();  //Return a piece of data;User::find()->all();  //Return all data;User::find()->count();  //Return the number of records;User::find()->average();  //Return 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();  //Return the query result of the first row and first column of the value;User::find()->column();  //Return the value of the first column in the query result;User::find()->exists();  //Returns a row of data that indicates whether the query result is included;

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.