yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.23k stars 6.92k forks source link

Getting unknown property exception while yii\db\ActiveQuery::populateRelation #16980

Open luciuz opened 5 years ago

luciuz commented 5 years ago

Hey everybody!

If we have multiple relation via more than 1 relation, we get an exception in yii\db\ActiveQuery::getModelKey($model, $attributes) when yii\db\ActiveQuery::populateRelation trying to get a $key for the $link.

Example:

class User extends yii\db\ActiveRecord
{
    //...

    public function getSessionPlayground()
    {
        return $this->hasOne(Playground::class, ['id' => 'playground_id'])
            ->viaTable('user_session via_user_session', ['id' => 'last_user_session_id'])           
    }

    public function getSesionPlaygroundType()
    {
        return $this->hasOne(PlaygroundType::class, ['id' => 'playground_type_id'])
            ->via('sessionPlayground');
    }

In this case $link = ['playground_id'] and \db\ActiveQuery::getModelKey(User, ['playground_id']) throwing an exception that playground_id is unknown property for User.

Q A
Yii version 2.0.15.1
PHP version 7.2.7
samdark commented 5 years ago

Would you please try dev-master?