我正在尝试使用范围按类别过滤产品,我通过内部加入类别来做到这一点 问题是它只有当我一起开启时才会起作用,这会混淆分页
我记得这样做没有问题 也尝试使用'on'=>而不是'condition'=>在with()定义中,工作几乎相同,只有后者更慢的db-wise我认为任何想法?
PS。遗憾的是,在类别上留下加入产品不是一种选择
<?php
/**
* ProductCategory model
*/
class ProductCategory extends CActiveRecord
{
//...
public function relations()
{
return array(
//...
'parent' => array(self::BELONGS_TO, 'ProductCategory', 'parentId'),
'children' => array(self::HAS_MANY, 'ProductCategory', 'parentId'),
//...
);
}
//...
}
/**
* Product model
*/
class Product extends CActiveRecord
{
//...
public function relations()
{
return array(
//...
'categoriesJunction' => array(self::HAS_MANY, 'ProductCategoriesProducts', 'productId'),
'categories' => array(self::HAS_MANY, 'ProductCategory', array('categoryId'=>'id'), 'through'=>'categoriesJunction'),
//...
);
}
/**
* Category scope
*
* @param mixed Category ids the product must belong to (int or array of int)
* @return \Product
*/
public function category($category = null) {
if ($category) {
$category = ProductCategory::model()->resetScope()->with('children')->findByPk($category);
if ($category) {
$categories = array($category->id);
if (is_array($category->children)) {
foreach ($category->children as $child) {
$categories[] = $child->id;
}
}
$this->getDbCriteria()->mergeWith(array(
'with' => array(
'categories' => array(
'on'=>'categories.id=' . implode(' or categories.id=', $categories),
'joinType' => 'inner join',
//'together'=>true,
),
),
));
}
}
return $this;
}
//...
}
答案 0 :(得分:0)
更新: select =&gt;假是要走的路
D.Mill 4月5日9:33
我赞同abos评论,这是如何搞乱分页的?也许您正在寻找'select'=&gt; false(在条件数组的'categories'部分内)
毕竟我找到了一个非常简单的解决方案,适用于范围,只需将范围标准部分更改为
$this->getDbCriteria()->mergeWith(array(
'with'=>array(
'categories'=>array(
'select'=>'false',
'on'=>'categories.id=' . implode(' or categories.id=', $categories),
'joinType'=>'inner join',
),
),
'together'=>true,
));