Home php教程 php手册 Summary of addition, deletion, modification and query of Yii2 framework database

Summary of addition, deletion, modification and query of Yii2 framework database

Sep 01, 2016 am 12:00 AM

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 sql statement to query all data in the user table;
User::findBySql('SELECT * FROM user')->one(); This method uses sql statement to query A piece of data in the user table;
User::find()->andWhere(['sex' => 'Female', 'age' => '18'])->count('id') ; //Count the total number of items that meet the conditions;
User::find()->one(); //Return one 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(); //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 value indicating whether the data row contains the query result;

Query operation:

User::find()->where(['name' => 'username'])->one(); This method returns a piece of data from ['name' => 'username'];
User::find()->where(['name' => 'username'])->all(); This method returns all data of ['name' => 'username'];

User::find()->andWhere(['sex' => 'Male', 'age' => '24'])->count('id'); Count the total number of items that meet the conditions Number;

New operation:

$model = newUser();
$model->username = 'username';
$model->age = '20';
$model->insert();

Modification operation:
$User = User::findOne($id);
$User->name = 'zhangsan';
$User->save(); // Equivalent to $User-> update();

Delete operation:

User::deleteAll('name = username'); Delete the data with name = username;
User::findOne($id)->delete(); Delete the database whose primary key is the value of the $id variable;
User:: deleteAll('age > :age AND sex = :sex', [':age' => '20', ':sex' => '1']); Delete data that meets the conditions;

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)