Add, delete, modify, and query operations for the Yii2.0 advanced framework database. Some operations for adding, deleting, modifying, and querying the Yii2.0 advanced framework Database. the yii2.0 addition and deletion yii2.0 framework is a highly efficient framework developed by PHP and brings together a lot of effort from the author, the following operations are performed to add, delete, modify, and query the Yii2.0 advanced framework database:
The yii2.0 framework is a highly efficient framework developed by PHP. it brings together a lot of painstaking efforts from the Author. the following uses the user as an example to explain 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.
The yii2.0 framework is a highly efficient framework developed by PHP. it brings together a lot of painstaking efforts from the author...