yii2: ExistValidator, targetRelation does not work when owner and related active record use different ActiveQuery implementation

What steps will reproduce the problem?

class Unit extends \yii2tech\filedb\ActiveRecord
{
}

class Product extends \yii\db\ActiveRecord
{
    public function getUnit()
    {
        return $this->hasOne(Unit::class, ['id' => 'unit_id']);
    }

    public function rules()
    {
        return [
            [['unit_id'], 'exist', 'targetRelation' => 'unit'],
        ];
    }
}

$product = new Product(['unit_id'=>1]);
$product->save();

What is the expected result?

No errors.

What do you get instead?

Calling unknown method: yii\db\Connection::getQueryProcessor()

It tries to use Product connection instead of Unit connection to read data.

It worked ok on 2.0.42.1

Additional info

Q A
Yii version 2.0.?
PHP version 7.4.21

| Operating system | Ubuntu 20.04

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Comments: 22 (17 by maintainers)

Most upvoted comments

@samdark this shouldn’t be closed as I understand, even after rollback (which is weird anyway because WinterSilence said that this is not a problem in his update - I agree with that - and yet he rolled it back) the problem is still here. I’ve tested it on a previous implementation.

I solved it by adding forceMasterDb = false. I wrote above, it looks like @WinterSilence fixed an old bug. My opinion is that you should set forceMasterDb = false by default several year ago (when the feature was implemented). It broke my code due to the fact that I did not study the validator well enough, and an error in the framework did not allow it to be immediately identified. Those on the one hand, it would be necessary to accept the fix, on the other hand, it can break the code of someone like me.

It worked on 2.0.42.1 because there was an error inside validator. My fault + yii2’s fault make my code work well.

the meaning of this issue is that the validator code is probably not sufficiently covered by tests.