Answer the question
In order to leave comments, you need to log in
How to get data on a related model, if null is returned in the cell of the desired relationship, while the required data is in _related?
I set out to implement the functionality of individual prices in Yii2 , and thanks to the active help of the community, I managed to come up with a solution, but there is one problem - the desired AR response cell contains null, but the private _releted property contains the correct answer.
We have an AR request:
Service::find()
->where(['active' => 1])
->orderBy(['price' => SORT_ASC])
->joinWith('individualPrice')
->one()
public function getIndividualPrice()
{
return $this->hasOne(UniqueService::class, ['service_id' => 'id'])->andWhere(['user_id' => \Yii::$app->user->id]);
}
object(common\models\Service)#132 (11) {
["individualPrice"]=>
NULL
["_attributes":"yii\db\BaseActiveRecord":private]=>
array(11) {
["id"]=>
int(1)
["group_service"]=>
int(1)
["name"]=>
string(12) "Some Service"
["description"]=>
string(11) "Service dec"
["price"]=>
float(0.02)
["min"]=>
int(1)
["max"]=>
int(50000)
["max_order"]=>
int(0)
["active"]=>
int(1)
}
["_oldAttributes":"yii\db\BaseActiveRecord":private]=>
array(11) {
["id"]=>
int(1)
["group_service"]=>
int(1)
["name"]=>
string(12) "Some Service"
["description"]=>
string(11) "Service dec"
["price"]=>
float(0.02)
["min"]=>
int(1)
["max"]=>
int(50000)
["max_order"]=>
int(0)
["active"]=>
int(1)
}
["_related":"yii\db\BaseActiveRecord":private]=>
array(1) {
["individualPrice"]=>
object(common\models\UniqueService)#148 (10) {
["_attributes":"yii\db\BaseActiveRecord":private]=>
array(4) {
["id"]=>
int(12)
["user_id"]=>
int(4296)
["service_id"]=>
int(1)
["price"]=>
float(1.44)
}
["_oldAttributes":"yii\db\BaseActiveRecord":private]=>
array(4) {
["id"]=>
int(12)
["user_id"]=>
int(4296)
["service_id"]=>
int(1)
["price"]=>
float(1.44)
}
["_related":"yii\db\BaseActiveRecord":private]=>
array(0) {
}
["_relationsDependencies":"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) {
}
["_eventWildcards":"yii\base\Component":private]=>
array(0) {
}
["_behaviors":"yii\base\Component":private]=>
array(0) {
}
}
}
["_relationsDependencies":"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) {
}
["_eventWildcards":"yii\base\Component":private]=>
array(0) {
}
["_behaviors":"yii\base\Component":private]=>
array(0) {
}
}
Answer the question
In order to leave comments, you need to log in
why joinWith? As I understand it, join is useless here, try this:
$tryam = Service::find()
->where(['active' => 1])
->orderBy(['price' => SORT_ASC])
->with('individualPrice')
->one();
print_r($tryam->individualPrice);
die();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question