CGridview条件显示相关的模型数据

时间:2014-04-06 16:05:42

标签: php mysql yii

我正在尝试按category_id

过滤gridview中的产品记录

我有三张桌子

  

ProductMst(产品,名称)

     

CategoryMst(CATEGORY_ID,category_nme)

     

PromotedUserProduct(category_id,product_id)====>联结表

productMst模型规则如下

   public function relations()
{

    return array(
        'categoryMsts' => array(self::MANY_MANY, 'CategoryMst', 'promoted_user_product(user_product_id, category_id)'),

    );
}

我的控制器代码如下

         $criteria=new CDbCriteria();
    $categoryCondition=array();
      if(isset($_GET['category_id']) && $_GET['category_id']!==""){     
        $categoryCondition=array('select'=>false,
                'condition'=>'categoryMsts.category_id=1',
                'joinType'=>'INNER JOIN ');
    }

    $criteria->with=array(
            'categoryMsts'=>$categoryCondition);



    $dataProvider=new CActiveDataProvider('UserProductDtl',array(
        'pagination'=>array(
            'pageSize'=>$pagesize,
            ),
        'criteria'=>$criteria,
        ));
    $this->render('index',
        array('dataProvider'=>$dataProvider));

我无法过滤记录,任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

更改此

$categoryCondition=array(
    'select'=>false,
    'condition'=>'category_id=:category_id',
    'params'=>['category_id'=>$_GET['category_id'],
    'joinType'=>'INNER JOIN '
);