PHP Development Framework YII Framework Tutorial (25) Database-query Builder Example

Source: Internet
Author: User
The previous article introduced PHP using DAO (Database Access object interface) to access the database, using DAO requires programmers to write SQL statements, for some complex SQL statements, Yii provides query Builder to help programmers generate SQL statements, query Builder provides an object-oriented approach to dynamically creating SQL statements, making an inappropriate comparison, with PHP's DAO and. Net DAO interfaces very type, and Query builder a bit like LINQ, although much smaller than LINQ. For some simple SQL queries, it is usually not necessary to use query Builder, such as querying the employee table in the previous article.

Using Query Builder has the following benefits compared to using SQL statements directly:

Supports dynamic creation of more complex SQL queries through programs.

Automatically adds quotation marks to the table names in the created SQL statement to avoid conflicts with SQL reserved identifiers.

Specify to add quotation marks for parameters and use parameter bindings as much as possible to reduce the risk of SQL injection.

Using Query Builder does not directly write SQL statements, but provides some degree of database abstraction, which facilitates the switching of database types.

This example queries Chinook's two tables, customer and employee, and queries the contact information for all employeeid=4 managed customers.

If you use SQL queries, you can write:

SELECT C.firstname, C.lastname, C.address,c.email. From customer Cinner Joinemployee EON C.supportrepid=e.employeeid

WHERE e.employeeid=4 This example uses Query Builder to create a SQL query that modifies the Indexaction method of Sitecontroller:

Public Function Actionindex () {$model = array (); $connection =yii::app ()->db; $command = $connection CreateCommand ()->select (' C.firstname, C.lastname, C.address,c.email ')->from (' Customer C ')->join (' Employee e ', ' C.supportrepid=e.employeeid ')->where (' e.employeeid=4 '); $dataReader = $command->query ();//each $ Row is an array representing a row of Dataforeach ($dataReader as $row) {$customer = new Datamodel (); $customer->firstname= $row [' FirstName ']; $customer->lastname= $row [' lastName ']; $customer->address= $row [' address ']; $customer email= $row [' Email ']; $model []= $customer;} $this->render (' index ', array (' model ' = $model,));}

You can see that query Builder is also using Cdbcommand, and Cdbcommand provides the following methods for querying data:

Select () selectdistinct () from () where () join () Leftjoin () Rightjoin () CrossJoin () Naturaljoin () group () have () Order () Limit () offset () union ()

In addition to the data definition method:

CreateTable () renametable () droptable () truncatetable () AddColumn () Renamecolumn () Altercolumn () DropColumn () is CreateIndex () Dropindex ()

You can see that the method supported by Cdbcommand basically corresponds to the keyword one by one of the SQL statement, so that without using Query Builder, you can use SQL statements directly for simple SQL statements, and query Builder for more complex queries.

This example shows the result:

The above is the PHP Development Framework Yii Framework Tutorial (25) Database-query Builder sample content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.