Programmatically Prepend Related Table Alias To Relation Condition In Yii2

Consider the following relation:




    public function getPrimaryUserProfile()

    {

        $query = $this->hasOne(UserProfile::className(), ['id' => 'user_profile']);

        $query->andOnCondition('`default_profile` = :defaultProfileId', [':defaultProfileId'=>1]);

        return $query;

    }



When I use JoinWith, this relation yields the following SQL:




SELECT `user`.* FROM `user`

LEFT JOIN `user_profile`

ON (`user`.`user_profile` = `user_profile`.`id`) AND (`default_profile` = 1)



I would like to fully qualify default_profile by programmatically prepending the related table name to the andOnCondition condition. I know that relations can be joined multiple times within a query and this requires using a table alias. Does Yii2 provide a method for programmatically retrieving the table alias of a relation name?

I’d like the SQL to read:




SELECT `user`.* FROM `user`

LEFT JOIN `user_profile`

ON (`user`.`user_profile` = `user_profile`.`id`) AND (`user_profile`.`default_profile` = 1)



Upon further inspection of Yii2’s core, it appears that eager loading uses multiple queries and no longer requires aliases in most cases. I’ll just use the table name from the ActiveRecord model.