Book Image

Mastering Yii

By : Charles R. Portwood ll
Book Image

Mastering Yii

By: Charles R. Portwood ll

Overview of this book

The successor of Yii Framework 1.1, Yii 2 is a complete rewrite of Yii Framework, one of the most popular PHP 5 frameworks around for making modern web applications. The update embraces the best practices and protocols established with newer versions of PHP, while still maintaining the simple, fast, and extendable behavior found in its predecessor. This book has been written to enhance your skills and knowledge with Yii Framework 2. Starting with configuration and how to initialize new projects, you’ll learn how to configure, manage, and use every aspect of Yii2 from Gii, DAO, Query Builder, Active Record, and migrations, to asset manager. You'll also discover how to automatically test your code using codeception. With this book by your side, you’ll have all the skills you need to quickly create rich modern web and console applications with Yii 2.
Table of Contents (20 chapters)
Mastering Yii
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
5
Modules, Widgets, and Helpers
13
Debugging and Deploying
Index

Query Builder


Building on top of the foundations laid by DAO is Yii's Query Builder. Yii's Query Builder allows us to write database-agnostic queries in a programmatic way. Consequently, queries written through the Query Builder are significantly more readable than their DAO counterparts.

The basics of Query Builder involve the creation an instance of yii\db\Query, the construction of a statement, and then the execution of that query statement. For example, we could simply query for all the users in our database in Query Builder using the following code:

$users = (new \yii\db\Query())
    ->select(['id', 'email'])
    ->from('user')
    ->all();

Tip

When working with Query Builder, we're actually using the yii\db\Query class rather than yii\db\QueryBuilder. While yii\db\QueryBuilder can generate SQL statements similar to those generated by yii\db\Query, yii\db\Query enables these statements to be database-agnostic. In general, you'll want to work with yii\db\Query when using Query Builder...