douge7771 2013-04-04 13:02
浏览 14
已采纳

不使用“在一起”的内连接关系

i am trying to filter products by category using a scope and i do that by inner joining the categories the problem is it only works when i turn together on, which messes up pagination

i recall doing that without a problem also tried using 'on'=> instead of 'condition'=> inside the with() definition, works pretty much the same only the latter is slower db-wise i think

any ideas?

ps. left joining products on a category is unfortunately not an option

<?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;
    }
    //...
}
  • 写回答

1条回答 默认 最新

  • doulangtiao4508 2013-04-05 12:40
    关注

    UPDATE: select => false is the way to go

    D.Mill Apr 5 at 9:33

    I agree with aboce comment, how does this mess up pagination? Maybe you are looking for 'select'=>false (inside 'categories' section of your criteria array)

    after all I found a quite simple solution that works well for a scope, just changing the scope criteria part to

    $this->getDbCriteria()->mergeWith(array(
        'with'=>array(
            'categories'=>array(
                'select'=>'false',
                'on'=>'categories.id=' . implode(' or categories.id=', $categories),
                'joinType'=>'inner join',
            ),
        ),
        'together'=>true,
    ));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据