Reputation: 714
I have a many to many relation in Yii2 and I want to set where clause but frameworks says : Unknown column!
here is my find code :
$rm = ProductHasAttValue::find()
->with('attValue')
->where('product_id='.$id." AND attValue.att_id=1")
->all();
I'm truly sure the attValue's table has a column with name :att_id.
why should I set this where correctly ?
thanks in advance ?
p.s: if I don't use that where clause and write this foreach I can get the att_id values ..
foreach($rm as $ziizii){
echo $ziizii->attValue->att_id."*";
}
model :
here is my whole modle class :<?php
namespace common\models;
use Yii;
/**
* This is the model class for table "att_value".
*
* @property integer $id
* @property integer $att_id
* @property string $value
*
* @property Att $att
* @property ProductHasAttValue[] $productHasAttValues
* @property Product[] $products
*/
class AttValue extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'att_value';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['att_id', 'value'], 'required'],
[['id', 'att_id'], 'integer'],
[['value'], 'string']
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'att_id' => 'Att ID',
'value' => 'Value',
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getAtt()
{
return $this->hasOne(Att::className(), ['id' => 'att_id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getProductHasAttValues()
{
return $this->hasMany(ProductHasAttValue::className(), ['att_value_id' => 'id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getProducts()
{
return $this->hasMany(Product::className(), ['id' => 'product_id'])->viaTable('product_has_att_value', ['att_value_id' => 'id']);
}
}
Upvotes: 0
Views: 221
Reputation: 1417
In where clause it shouldn't be
->where('product_id='.$id." AND attValue.att_id=1")
But
->where('product_id='.$id." AND att_value.att_id=1")
You should replace the relation name attValue
with related table name in where clause
Upvotes: 3