yii2.0 Framework is a PHP development of a more efficient framework, the collection of the author of a large number of painstaking efforts, the following through the user as an example to everyone detailed yii2 use of some basic additions and deletions to check operations.
User::find ()->all (); Returns all user data;
User::findone ($id); Returns a data of the primary key id=1;
User::find ()->where ([' Name ' => ' TTT '])->one (); Returns a piece of data [' name ' => ' TTT '];
User::find ()->where ([' Name ' => ' TTT '])->all (); Returns all data of [' Name ' => ' TTT '];
User::findbysql (' SELECT * from user ')->all (); Use SQL statements to query all data in the user table;
User::findbysql (' SELECT * from user ')->one (); This method uses SQL statements to query a data in the user table;
User::find ()->andwhere ([' Sex ' => ' female ', ' age ' => '])->count (' id '); (a) Statistics of the total number of eligible articles;
User::find ()->one (); Returns a piece of data;
User::find ()->all (); return all data;
User::find ()->count (); Returns the number of records;
User::find ()->average (); Returns the average of the specified column;
User::find ()->min (); Returns the minimum value of the specified column;
User::find ()->max (); Returns the maximum value of the specified column;
User::find ()->scalar (); Returns the query result of the first row of the value;
User::find ()->column (); Returns the value of the first column in the query result;
User::find ()->exists (); Returns a value indicating whether a row of data is included in the query result;
YII2 's group query, taking the user as an example:
User::find ()->addgroupby (' title ')->all (); grouped by title
1. Add (Insert)
$model = new User ();
$model->username = ' lad ';
$model->insert ();
To delete some simple operations on the database, or the old way, above I write code, if the style confusion, I will attach a screenshot. Or use the user table as an example
User::d eleteall (' name = Chap '); Delete the data for name = Lad;
User::findone ($id)->delete (); Deletes a database with a primary key of $id variable value;
User::d eleteall (' Age >: Age and sex =: Sex ', [': Age ' => ', ': Sex ' => ' 1 ']); Delete the eligible data;
Recommended reading: Learn from scratch YII2 Framework (i) through the composer installation Yii2 framework, I believe that we learn yii2 will be helpful.