Olga
Olga

Reputation: 443

Sql query in yii2

I would like to execute such a command:

SELECT number.phone_number 
FROM number 
LEFT JOIN ordered_number 
ON ordered_number.number_id=number.id 
WHERE ordered_number.order_id=123

with Yii2. I do this:

$numery = Number::find()
                    ->select('number.phone_number')
                    ->leftJoin('ordered_number', ['ordered_number.number_id' => 'number.id'])
                    ->where(['ordered_number.order_id' => 123])
                    ->createCommand()
                    ->rawSql;

But I get then this:

'SELECT `number`.`phone_number` FROM `number` LEFT JOIN `ordered_number` ON `ordered_number`.`number_id`='number.id' WHERE `ordered_number`.`order_id`=123' 

Which doesn't work. When I put the first thing right into my base, I get 4 results, which is correct. But when I do (I think exactly the same) with Yii, I have troubles, because it is null. The only differences between what I have written and what appears after rawSql, are ` and '.

Upvotes: 1

Views: 67

Answers (1)

Bizley
Bizley

Reputation: 18021

Sorry, I've just read docs for this method - it should be:

->leftJoin('ordered_number', 'ordered_number.number_id = number.id')

because using the where() kind of conditions with array leads to comparing field to string.

Upvotes: 2

Related Questions