There is a table of records (items), a table of images (images), and a table of links (items_images). Those. One-to-many connection is obtained (one entry has several images). View of the items_images table

item_id | image_id | sort 

I need to get the value of the sort field for each image, I do this

 public function getImages() { return $this->hasMany( Image::className(), ['id' => 'image_id']) ->viaTable('{{%items_images}}', ['item_id' => 'id'] ) ->leftJoin('{{%items_images}}', ['{{%items_images}}.item_id' => $this->id]) ->onCondition(['item_id' => $this->id] ) ->select('*'); } 

Displays all fields from the Image model, i.e. as needed, but there is no field c in the items_images table:


Corrected request

 public function getImages() { return $this->hasMany( Image::className(), ['id' => 'image_id']) ->viaTable('{{%items_images}}', ['item_id' => 'id'] ) ->leftJoin('{{%items_images}}', '{{%items_images}}.image_id = {{%images}}.id') ->select('*'); } 

If you choose as an array, i.e.

 Item::find()->where( 'id = :id', [ 'id' => $id ] )->with('images')->asArray()->one() 

Chooses as it should.

If you remove asArray() , then item_id, image_id and sort do not return. Why is that ?

    1 answer 1

    So works

     public function getImages() { return $this->hasMany( Image::className(), ['id' => 'image_id']) ->viaTable('{{%items_images}}', ['item_id' => 'id'] ) ->leftJoin('{{%items_images}}', '{{%items_images}}.image_id = id') ->asArray() ->select('*'); } 

    In another way it is not clear how to do it.