There is an Image model in which images are stored, as the name implies. Each individual image is attached to any other model by specifying the modelName and itemId fields .

I can not figure out how to make a greedy sample. For linked tables through FK, you can make selections through hasOne and hasMany , but I cannot understand how to do this without this connection. As it is a pity for each model to make one extra request.

The image module itself for yii2 downloaded and installed separately, you can look at the yii2-images repository here .

  • one
    Have you tried this solution? github.com/CostaRico/yii2-images/issues/… - Zhukov Roman
  • I tried something similar, did not work, now I tried this solution, it also does not work - arkadij_ok
  • although for some reason the model itself has no images associated with it, but there are fewer requests to the database, hmm - arkadij_ok
  • one
    instead of where you can use onCondition - Bookin
  • one
    here is the final solution return $this->hasOne(Image::className(), ['itemId' => 'id']) ->onCondition(['modelName' => $this->tableName()]); - arkadij_ok

1 answer 1

Reply from comments

Instead of the where() method, use onCondition()

 return $this->hasOne(Image::className(), ['itemId' => 'id']) ->onCondition(['modelName' => $this->tableName()]);