0

スコープを使用してカテゴリ別に製品をフィルタリングしようとしていますが、カテゴリを内部結合することでそれを行います。問題は、一緒にオンにしたときにのみ機能し、ページネーションが台無しになることです

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;
    }
    //...
}
4

1 に答える 1

0

更新: select => false が進むべき道です

D.Mill 4 月 5 日 9:33

上記のコメントに同意しますが、これはどのようにページネーションを台無しにしますか? 'select'=>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,
));
于 2013-04-05T12:40:39.963 に答える