yii app db createcommand join yii1 1

yii app db createcommand join yii1

$user = Yii::app()->db->createCommand()
    ->select('id, username, profile')
    ->from('tbl_user u')
    ->join('tbl_profile p', 'u.id=p.user_id')
    ->where('id=:id', array(':id'=>$id))

Here is what the above code is Doing:
1. We create a command object using Yii::app()->db->createCommand().
2. We call the select() method to specify the SELECT clause.
3. We call the from() method to specify the FROM clause.
4. We call the join() method to specify the JOIN clause.
5. We call the where() method to specify the WHERE clause.
6. We call the queryRow() method to execute the SQL statement and return a single row of result.

The above code is equivalent to the following SQL statement:

SELECT id, username, profile FROM tbl_user u INNER JOIN tbl_profile p ON u.id=p.user_id WHERE id=:id

The queryRow() method will return false if no row is found.

Note: The queryRow() method is available since version 1.1.3.

Similar Posts