简体   繁体   English

Yii2 得到 model 关系,字面意思是“关系”

[英]Yii2 get model relation named "relation" literally

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.这是一个很棒的方法ActiveRecord->getRealtion($name)如果 model 与$name有关系,则返回ActiveQuery ,如果没有,则返回null 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 :但它不会那么令人困惑,如果它会触发某种异常或某些东西,由框架处理,但它会抛出 PHP 的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?它是 PHP 中的保留字符串,还是某种 PHP 行为,还是什么? Please, explain me, what is going on.请解释一下,这是怎么回事。

Oh, I've realized that.哦,我已经意识到了。 It is Yii2 thing: BaseActiveRecord->getRelation have such code:它是 Yii2 的东西: BaseActiveRecord->getRelation有这样的代码:

    $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.$name === 'relation'情况下,它将是$getter === 'getRelation' ,它将调用$this->getRelation()本身而没有任何属性,这会导致 PHP 错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM