Answer the question
In order to leave comments, you need to log in
YII2 How to correctly build a query with joinWith?
The bottom line: I get $id
, I find it in the product_image table 'id' => $id
, and I also need a record from the table product
, which should have 'id' => 'product_image->id'
. You need to find one picture record and one product record.
How to arrange it?
Did it like this:
$model = Images::find()
->joinWith('products')
->where(['id' => $id])
->andWhere(['id' => 'product_id'])
->one();
Answer the question
In order to leave comments, you need to log in
$model = Images::find()
->joinWith('product')
->where(['product.id' => $id])
->asArray()
->one();
Obviously, the image-product connection is not described, otherwise the product would not be "got" through Products::find(), but like $model->product. Here is the detail. In your case you need something like
class Images extends ActiveRecord
{
// ...
public function getProduct()
{
return $this->hasOne(Products::className(), ['id' => 'product_id']);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question