1

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 '.

2
  • Is it still null if you remove ->select('number.phone_number') so all fields are returned (and of course with all() at the end instead of ->createCommand()->rawSql) Commented Dec 20, 2016 at 10:43
  • Yes, I still get empty array Commented Dec 20, 2016 at 10:47

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.