Home > php教程 > php手册 > body text

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

WBOY
Release: 2016-09-01 00:00:46
Original
789 people have browsed it

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;

source:php.cn
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template