Yii provides a basic database access layer as was described in [Database basics](database-basics.md) section. Still it's
a bit too much to use SQL directly all the time. To solve the issue Yii provides a query builder that allows you to
work with the database in object-oriented style.
Yii provides a basic database access layer as described in the [Database basics](database-basics.md) section. The database access layer provides a low-level way to interact with the database. While useful in some situations, it can be tedious to rely too much upon direct SQL. An alternative approach that Yii provides is the Query Builder. The Query Builder provides an object-oriented vehicle for generating queries to be executed.
Basic query builder usage is the following:
Here's a basic example:
```php
$query=newQuery;
// Define query
// Define the query:
$query->select('id, name')
->from('tbl_user')
->limit(10);
// Create a command. You can get the actual SQL using $command->sql
// Create a command.
$command=$query->createCommand();
// Execute command
// You can get the actual SQL using $command->sql
// Execute the command:
$rows=$command->queryAll();
```
Basic selects and joins
-----------------------
Basic selects
-------------
In order to form a `SELECT` query you need to specify what to select and where to select it from.
In order to form a basic `SELECT` query, you need to specify what columns to select and from what table:
```php
$query->select('id, name')
->from('tbl_user');
```
If you want to get IDs of all users with posts you can use `DISTINCT`. With query builder it will look like the following:
Select options can be specified as a comma-separated string, as in the above, or as an array. The array syntax is especially useful when forming the selection dynamically:
The first argument is the join type to perform. The second is the table to join to, and the third is the condition.
Specifying SELECT conditions
---------------------
Usually you need data that matches some conditions. There are some useful methods to specify these and the most powerful
is `where`. There are multiple ways to use it.
Usually data is selected based upon certain criteria. Query Builder has some useful methods to specify these, the most powerful of which being `where`. It can be used in multiple ways.
The simplest is to specify condition in a string:
The simplest way to apply a condition is to use a string:
When using this format make sure you're binding parameters and not creating a query by string concatenation.
When using strings, make sure you're binding the query parameters, not creating a query by string concatenation. The above approach is safe to use, the following is not:
Instead of binding status value immediately you can do it using `params` or `addParams`:
```php
$query->where("status=$status");// Dangerous!
```
Instead of binding the status value immediately, you can do so using `params` or `addParams`:
```php
$query->where('status=:status');
$query->addParams([':status'=>$status]);
```
There is another convenient way to use the method called hash format:
Multiple conditions can simultaneously be set in `where` using the *hash format*: