yii2 多对多 where 子句
yii2 many to many where clause
我在 Yii2 中有一个多对多的关系,我想设置 where 子句但是框架说:未知列!
这是我的查找代码:
$rm = ProductHasAttValue::find()
->with('attValue')
->where('product_id='.$id." AND attValue.att_id=1")
->all();
我确定 attValue 的 table 有一个名称为 :att_id.
的列
为什么我应该正确设置它?
提前致谢?
p.s:
如果我不使用那个 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']);
}
}
在 where 子句中不应该是
->where('product_id='.$id." AND attValue.att_id=1")
但是
->where('product_id='.$id." AND att_value.att_id=1")
您应该在 where 子句
中将关系名称 attValue
替换为相关的 table 名称
我在 Yii2 中有一个多对多的关系,我想设置 where 子句但是框架说:未知列!
这是我的查找代码:
$rm = ProductHasAttValue::find()
->with('attValue')
->where('product_id='.$id." AND attValue.att_id=1")
->all();
我确定 attValue 的 table 有一个名称为 :att_id.
的列为什么我应该正确设置它?
提前致谢?
p.s: 如果我不使用那个 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']);
}
}
在 where 子句中不应该是
->where('product_id='.$id." AND attValue.att_id=1")
但是
->where('product_id='.$id." AND att_value.att_id=1")
您应该在 where 子句
中将关系名称attValue
替换为相关的 table 名称