active-record
active-record copied to clipboard
FR ActiveRecod relation parameters in with() call as array
Feature Request. Hello. I'm using AR and created a relation for current user's relational record in another table (user's vote result of poll). Relation looks this way:
/**
* @return \yii\db\ActiveQuery
*/
public function getUserPollStatus($id)
{
return $this->hasOne(UserPollStatusItem::className(),
['poll_id' => 'id'])
->where(['user_id' => $id])
->limit(1);
}
I was expecting that could be able to enable eager loading for this query and that this will look like:
if ($user_id !== null)
$query->with(['userPollStatus' => [$user_id]]);
But it looks that this doesn't work, it supports only callbacks. Also, it does not check really passed value.
What about adding ability to pass an array instead of callback to modify relation query?
A callback is the most flexible way of modifying a query, what would be the advantage of passing an array? Also, is the class you have created the relation in a User or something else?
@cebe the advantage is an eager loading of parametrized relational data without anonymous callback. The scheme is following:
Table: users Table: users_poll_statuses
---------------- -----------------------------------
id --------------> user_id
... id
... ...
I propose to implement a shortcut. It's just shortier to write 'relationName' => [$argument]
than write again all conditions in callback.
Thanks, its clear now.