Summary of simple syntax for adding, deleting, modifying, and querying the Yii2 framework Database
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;
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 '=> 'mal', 'age' => '24'])-> count ('id '); count the total number of eligible items;
Add operation:
$ Model = newUser ();
$ Model-> username = 'username ';
$ Model-> age = '20 ';
$ Model-> insert ();
Modification Operation:
$ User = User: findOne ($ id );
$ User-> name = 'hangsan ';
$ User-> save (); // equivalent to $ User-> update ();
Delete operation:
User: deleteAll ('name = username'); Delete data with name = username;
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;
The above is a summary of the simple syntax for adding, deleting, modifying, and querying the Yii2 framework database. I hope it will help you. If you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!