Yii搜索重复的结果

hzbexzde  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(151)

"我是Yii的新手,这不是我的代码"
当我搜索某个东西时,结果是重复的,像结果:产品1产品2产品2
我敢肯定,问题是来自控制器,因为我检查了视图.控制器:

public function actionIndex($q){

  $criteria=new CDbCriteria;
      $criteria->with=array('cat','postDesc');
  $criteria->together=true;
 // $criteria->compare('post.pack_type',2);
 $criteria->compare('cat.zone','Group',true);
 $criteria->compare('postDesc.name',$q,true);

 // $criteria->compare('category_id',$id); 

     $this->pageTitle = "The search resluts for:".$q;
Yii::app()->clientScript->registerMetaTag(Helpers::config('meta_keyword'), 'keywords');
Yii::app()->clientScript->registerMetaTag(Helpers::config('meta_description'), 'description');

$provider=new CActiveDataProvider('PostCategory', array(
         'pagination'=>array(
    'pageSize'=> Yii::app()->user->getState('pageSize',Yii::app()->params['defaultPageSize']),
),

        'criteria'=>$criteria,
         'sort' => array(
            'defaultOrder' => 't.post_id desc',
        ),

    ));

 $this->render('index',array(

        'dataProvider'=>$provider,
        'cat'=>$cat

    ));

}

public function actionItem($id){
    $model=Post::model()->findByPK((int)$id);

     $this->pageTitle = $model->desc->name;
Yii::app()->clientScript->registerMetaTag(Helpers::config('meta_keyword'), 'keywords');
Yii::app()->clientScript->registerMetaTag(Helpers::config('meta_description'), 'description');

    $images=PostImage::model()->findAll(array('condition'=>'post_id='.$id));
    $attrs=PostAttr::model()->findAll(array('condition'=>'post_id='.$id,'order'=>'post_attr_id'));
    $media=PostMedia::model()->findAll(array('condition'=>'post_id='.$id));
     $this->render('item',array(

        //'dataProvider'=>$provider,
        'model'=>$model,
        'images'=>$images,
        'attrs'=>$attrs,
        'media'=>$media,

    ));

}

因此,我希望它发送的不是两个,而是每个产品的一个结果。

vptzau2j

vptzau2j1#

如果您的模型包含多个postDesccat相关记录,则可能会发生这种情况。解决方案可能会因您在这种情况下希望获得的结果而异,但最简单的解决方案可能是按PostCategory私钥字段分组:

$criteria->group = 't.id';

相关问题