Yii2.0 advanced framework database addition, deletion, modification, and query operations, yii2.0 addition, deletion, and PHP Tutorial

Source: Internet
Author: User
Tags findone
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...

Related Article

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.