i would like to know whether i can give a relation a specific name when using hasMany?
example: say i have a Document model which has children documents like
final class Document extends ActiveRecord {
public function getChildrenQuery(): DocumentQuery {
return $this->hasMany(self::class, ['parent_id' => 'id']);
}
}
and when i actually get the data i get:
[
'id' => 100,
'name' => 'some document name',
'childrenQuery' => [ ... children here ....]
]
is there a way to name that relation children instead of childrenQuery?
please dont tell me to rename the method cause that makes code a mess when you call getChildren but get an absolutely unrelated query object
There is a difference between calling it this way->getChildrenQuery() and this ->childrenQuery - this is how the framework works, so basically calling this method with Query suffix is on you. Just call it getChildren. And if you don’t want to do this create method like
public function getChildren()
{
return $this->getChildrenQuery();
}
which is pointless but then you can have your method not renamed and also have ->children.