yiiduplicatesyii1.x

Yii searching duplicated results


I'm new to Yii and this is not my code

When i search something, the results are duplicated, Like Results: product1 product1 product2 product2

And I'm sure that the problem is coming from the Controller because i checked the view. The Controller:

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,

    ));

}

So I want it to send not two, but one result of every product.


Solution

  • It may happen if your model contains multiple postDesc or cat related records. Solution may differ depending on what result you want to achieve in this situation, but the simplest one is probably group by PostCategory private key field:

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