The yii2.0 framework is a highly efficient framework developed by PHP, the following uses the user as an example to explain how to add, delete, modify, and query the yii2.0 advanced framework database. The yii2.0 framework is a highly efficient framework developed by PHP and brings together a lot of effort from the author, the following describes some basic addition, deletion, modification, and query operations in 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 ['name' => 'ttt;
User: find ()-> where (['name' => 'ttt'])-> all (); // return all data of ['name' => 'ttt;
User: findBySql ('select * FROM user')-> all (); // use an SQL statement to query all data in the user table;
User: findBySql ('select * FROM user')-> one (); this method uses an 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 condition;
User: find ()-> one (); // return a data record;
User: find ()-> all (); // return all data;
User: find ()-> count (); // number of records returned;
User: find ()-> average (); // returns the average value of the specified column;
User: find ()-> min (); // return the minimum value of the specified column;
User: find ()-> max (); // returns the maximum value of the specified column;
User: find ()-> scalar (); // query results in the first column of the first row of the returned value;
User: find ()-> column (); // return the value of the first column in the query result;
User: find ()-> exists (); // return a value indicating whether the data row contains the query result;
The group query of yii2 takes the user as an example:
User: find ()-> addGroupBy ('title')-> all (); group by title
1. Add (insert)
$ Model = new User ();
$ Model-> username = 'hangzhou ';
$ Model-> insert ();
Some simple operations on Database deletion are still the same. I wrote the code above. If the style is messy, I will attach it. Take the user table as an example.
User: deleteAll ('name = Your Child '); Delete data with name = your child;
User: findOne ($ id)-> delete (); delete a database whose primary key is the $ id variable value;
User: deleteAll ('Age>: age AND sex =: sex ', [': age' => '20', ': sex' => '1']); delete Qualified Data;
Recommended reading: learning the YII2 framework from scratch (1) Installing the Yii2 framework through Composer will help you learn yii2.