Set different relation name for hasMany()?

hey

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

thanks in advance!

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.

i think i was pretty clear about why i absolutely do not want to call the method getChildren

also accessing properties directly bypassing a getter is a huge no-no in my book

You should use a different framework then.

but yii2 is perfect otherwise, what a shame :frowning: