db\Query from()

from() public method

Sets the FROM part of the query.

public $this from ( $tables )
$tables string|array

The table(s) to be selected from. This can be either a string (e.g. 'user') or an array (e.g. ['user', 'profile']) specifying one or several table names. Table names can contain schema prefixes (e.g. 'public.user') and/or table aliases (e.g. 'user u'). The method will automatically quote the table names unless it contains some parenthesis (which means the table is given as a sub-query or DB expression).

When the tables are specified as an array, you may also use the array keys as the table aliases (if a table does not need alias, do not use a string key).

Use a Query object to represent a sub-query. In this case, the corresponding array key will be used as the alias for the sub-query.

Here are some examples:

// SELECT * FROM  `user` `u`, `profile`;
$query = (new \yii\db\Query)->from(['u' => 'user', 'profile']);

// SELECT * FROM (SELECT * FROM `user` WHERE `active` = 1) `activeusers`;
$subquery = (new \yii\db\Query)->from('user')->where(['active' => true])
$query = (new \yii\db\Query)->from(['activeusers' => $subquery]);

// subquery can also be a string with plain SQL wrapped in parenthesis
// SELECT * FROM (SELECT * FROM `user` WHERE `active` = 1) `activeusers`;
$subquery = "(SELECT * FROM `user` WHERE `active` = 1)";
$query = (new \yii\db\Query)->from(['activeusers' => $subquery]);
return $this

The query object itself

doc_Yii
2016-10-30 16:58:59
Comments
Leave a Comment

Please login to continue.