0
votes

Am confused as duck. It is a great method ActiveRecord->getRealtion($name) which returns ActiveQuery if model have relation with $name and null if it haven't. And this method works perfectly for almost every name, but... "relation" literally.

But it would not be so confusing, if it would trigger some kind of exceptions or something, handled by framework, but it throws PHP's ArgumentCountError:

Too few arguments to function yii\db\BaseActiveRecord::getRelation(), 0 passed in /usr/src/app/vendor/yiisoft/yii2/db/BaseActiveRecord.php on line 1244 and at least 1 expected

Really, this string of code

$models->getRelation('relation')

would throw that error and I do not know why. What am I missing? Is it a reserved string in PHP, or some kind of PHP behavior, or what? Please, explain me, what is going on.

1

1 Answers

0
votes

Oh, I've realized that. It is Yii2 thing: BaseActiveRecord->getRelation have such code:

    $getter = 'get' . $name;
    try {
        // the relation could be defined in a behavior
        $relation = $this->$getter();
    } catch (UnknownMethodException $e) {
    ...

In case of $name === 'relation', it would be $getter === 'getRelation', which would call $this->getRelation() itself without any attributes, and this is causing an PHP error.