YII2 Framework database simple additions and deletions to check grammar summary _php example

Source: Internet
Author: User
Tags findone first row

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;

Query action:

User::find ()->where ([' Name ' => ' username '])->one (); This method returns a piece of data [' name ' => ' username '];

User::find ()->where ([' Name ' => ' username '])->all (); This method returns all data for [' Name ' => ' username '];

User::find ()->andwhere ([' Sex ' => ' man ', ' age ' => '])->count (' id '); (a) Statistics of the total number of eligible articles;

New action:

$model = NewUser ();

$model->username = ' username ';

$model->age = ' 20 ';

$model->insert ();

To modify an operation:

$User = User::findone ($id);

$User->name = ' Zhangsan ';

$User->save (); Equivalent to $User->update ();

Delete action:

User::d eleteall (' name = Username '); Deletes the data of name = Username;

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;

The above is a small set to introduce the YII2 framework database simple additions and deletions to check grammar summary, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.