I have the following code where I try to return all the model attribute plus relation attributes:
User::find()
->joinWith("document user_doc")
->joinWith("document user_doc")
->select([User::tableName() . ".*", "user_doc.image as profile_picture", "user_doc.image as driver_license"])
->where(['sate_id' => $id, 'user_doc.type' => 'profile_picture', 'user_doc.type' => 'driver_license'])
->all();
In the select, I try to bring all user model attributes and 2 attributes coming from the relation. The sql sentence is ok and from mariadb I can get what I need but not else in Yii2, because Yii2 only returns the model attributes.
If I try to run as query command, the sql sentence won’t be well formed, for instance, "{{%user}}.id" => "profile_pic.id_user"
will be printed as
`user`.`id` = 'user_doc.id_user'
So, the sentence is trying to compare an integer field with a literal ‘user_doc.id_user’ instead with the field id_user from the alias table user_doc.
How can I to bring both, the model attributes and the relation attributes?