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.