スコープを使用してカテゴリ別に製品をフィルタリングしようとしていますが、カテゴリを内部結合することでそれを行います。問題は、一緒にオンにしたときにのみ機能し、ページネーションが台無しになることです
with() 定義内で 'condition'=> の代わりに 'on'=> を使用しようとしても、問題なくそれを実行したことを思い出します。ほとんど同じように機能しますが、後者は db の方が遅いと思います
何か案は?
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;
}
//...
}