Home > Backend Development > PHP Tutorial > Some operations of adding, deleting, modifying and querying the Yii2.0 advanced framework database, yii2.0 adding and deleting_PHP tutorial

Some operations of adding, deleting, modifying and querying the Yii2.0 advanced framework database, yii2.0 adding and deleting_PHP tutorial

WBOY
Release: 2016-07-12 09:04:39
Original
1030 people have browsed it

Yii2.0 advanced framework database addition, deletion, modification and query operations, yii2.0 addition and deletion

yii2.0 framework is a relatively efficient framework developed by PHP, which brings together the author After a lot of hard work, the following uses users as examples to explain in detail some basic addition, deletion, modification and search operations in the use of yii2.

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'); //Statistics of the total number of people who meet the conditions Number of items;
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 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;

Yii2 group query, taking user as an example:

User::find()->addGroupBy('title')->all();Group according to title

1. Add (insert)

$model = new User();
$model->username = 'boy';
$model->insert();

Some simple operations of database deletion are still the same. I wrote the code above. If the style is confusing, I will attach a screenshot. Let’s use the user table as an example

User::deleteAll('name = young man'); Delete the data of name = young man;
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;

Recommended reading: Learning the YII2 framework from scratch (1) Installing the Yii2 framework through Composer, I believe it will be helpful for everyone to learn yii2.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1072190.htmlTechArticle Some operations of Yii2.0 advanced framework database addition, deletion, modification and query, yii2.0 addition and deletion yii2.0 framework is developed by PHP A relatively efficient framework that combines a lot of efforts of the author. The following is provided by users...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template