Some operation _php examples of Yii2.0 advanced frame database additions and deletions

Source: Internet
Author: User
Tags findone

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.

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.