P
P
Padre2017-07-16 09:47:17
Yii
Padre, 2017-07-16 09:47:17

How to get response in Query Builder?

Have a request

$query = Country::find();
$SubQuery = $query->select(['id'])->from('partner')->where(['phone' => 9301111111])->createCommand()->rawSql;
echo '<pre>'; var_dump($SubQuery); echo '</pre>';

As a result, the correct answer is found in the $SubQuery variable, i.e. request:
string(51) "SELECT `id` FROM `partner` WHERE `phone`=9301111111"

If we execute this query in the database, we will get the result - one line id=1
Now we will put ->one() so that it gives the result, and does not show the raw query:
$SubQuery = $query->select(['id'])->from('partner')->where(['phone' => 9301111111])->one();

Now var_dump($SubQuery) shows us:
object(app\models\Country)#96 (8) {
["_attributes":"yii\db\BaseActiveRecord":private]=>
array(0) {
}
["_oldAttributes":"yii\db\BaseActiveRecord": private]=>
array(0) {
}
["_related":"yii\db\BaseActiveRecord":private]=>
array(0) {
}
["_errors":"yii\base\Model":private]=>
NULL
["_validators":"yii\base\Model":private]=>
NULL
["_scenario":"yii\base\Model":private]=>
string(7) "default"
["_events":"yii \base\Component":private]=>
array(0) {
}
["_behaviors":"yii\base\Component":private]=>
array(0) {
}
}

Upon closer examination, it is clear that not a single element of the object contains the desired value in the form of a unit. Why is that, what is wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Arman, 2017-07-16
@Arik

It seems that the ID refers to the primary key and it is not in the attributes, and you specified only its selection in the select; If you just need id, then $SubQuery->id, if there are other fields, then you need to list them in the select or remove them altogether, then it will return all the fields. It's not clear what you want

M
Maxim Timofeev, 2017-07-16
@webinar

If you select by "phone", then why is it not in the select?
Try:

$model = Country::find()
->select(['id','phone'])
->from('partner')
->where(['phone' => 9301111111])
->one();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question