【发布时间】:2018-12-17 11:39:16
【问题描述】:
我有一个模型 item,它与 image 模型有 hasMany 关系。在GridView 项目中,我有一列显示item 有多少图像。我希望能够按此列排序,并且可能能够使用复选框进行过滤以仅显示 items 而没有 images。
我尝试在搜索模型中添加检查以查询 with 与 images 关系,我想我会添加一个 andFilterWhere 以用于 image 计数小于输入值的位置。但是我不确定拥有该逻辑的最佳方法,是否有更好的 Yii 方法来按该计数进行过滤?
更新
根据this question 的信息,我更新了我的item 模型,使其具有返回关系计数的方法。
然后我像这样更新我的搜索模型:
class ItemSearch extends Item
{
public $image_count;
/**
* @inheritdoc
*/
public function rules()
{
return [
[['id', 'product_id', 'manufacturer_id', 'scale_id', 'owner_id', 'quantity'], 'integer'],
[['sku_number', 'description', 'details', 'condition'], 'safe'],
[['price', 'sale_price', 'image_count'], 'number'],
];
}
/**
* @inheritdoc
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
$query = Item::find();
$subQuery = Image::find()->select('item_id, COUNT(id) as image_count')->groupBy('item_id');
$query->leftJoin(['imageCount' => $subQuery], 'imageCount.item_id = id');
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$dataProvider->setSort([
'attributes' => [
'id',
'product_id',
'manufacturer_id',
'scale_id',
'owner_id',
'image_count' => [
'asc' => ['imageCount.image_count' => SORT_ASC],
'desc' => ['imageCount.image_count' => SORT_DESC],
'label' => 'Images'
]
]
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
// grid filtering conditions
if (!empty($this->id)) {
$query->andFilterWhere([
'id' => $this->id,
]);
}
if (!empty($this->product_id)) {
$query->andFilterWhere([
'product_id' => $this->product_id,
]);
}
if (!empty($this->manufacturer_id)) {
$query->andFilterWhere([
'manufacturer_id' => $this->manufacturer_id,
]);
}
if (!empty($this->scale_id)) {
$query->andFilterWhere([
'scale_id' => $this->scale_id,
]);
}
if (!empty($this->owner_id)) {
$query->andFilterWhere([
'owner_id' => $this->owner_id,
]);
}
if (!empty($this->image_count)) {
$query->andWhere(['is','imageCount.image_count',new \yii\db\Expression('null')]);
}
$query->andFilterWhere(['like', 'sku_number', $this->sku_number])
->andFilterWhere(['like', 'description', $this->description])
->andFilterWhere(['like', 'details', $this->details])
->andFilterWhere(['like', 'condition', $this->condition]);
return $dataProvider;
}
}
现在的问题是上面会产生一个类似这样的查询:
SELECT `item`.* FROM `item` LEFT JOIN (SELECT `item_id`, COUNT(id) as image_count FROM `image` GROUP BY `item_id`) `imageCount` ON imageCount.item_id = id WHERE `imageCount`.`image_count`=0
不再显示具有 0 个 images 的 items 的问题(并且选中该框不会显示任何内容),因为联接在 image 表中找不到该 ID 的任何内容
【问题讨论】:
-
您可能需要添加您的搜索模型和/或与您的问题相关的任何其他代码。这样如果你犯了任何错误,可以指出来。
-
@MuhammadOmerAslam 我还没有修改 Gii 生成的搜索模型。我去添加了andFilterWhere,但不知道该放什么。
-
@MuhammadOmerAslam 这真的很接近,我会更新我的问题。现在问题是
MySQL一个 -
请参阅我的回答中的 Update 部分。它现在应该不惜一切代价工作。