我在Yii2中有一个多对多的关系,我想设置where子句,但是框架说:未知列!
以下是我的查找代码:
$rm = ProductHasAttValue::find()
->with('attValue')
->where('product_id='.$id." AND attValue.att_id=1")
->all();我确信attValue的表中有一个名为:att_id的列。
为什么我要在正确的地方设置它?
提前谢谢?
附注:如果我不使用where子句,而编写这个foreach,我就可以获得att_id值。
foreach($rm as $ziizii){
echo $ziizii->attValue->att_id."*";
}型号:
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']);
}
}发布于 2016-07-16 00:09:13
在where子句中它不应该是
->where('product_id='.$id." AND attValue.att_id=1") 但
->where('product_id='.$id." AND att_value.att_id=1") 在where子句中,应将关系名称attValue替换为相关的表名
https://stackoverflow.com/questions/38400605
复制相似问题