CActiveRecord -> with() & counting & Pagination & Filter & Composter PK

Hello I would like to do a search on an attribute of my model that references the other model how to do?

I have the following code


  public function relations() {

    return array(



      'joinType'=>'INNER JOIN',





      'joinType'=>'INNER JOIN',

      'order'=>'??.dtenvio DESC',





Controller :

  public function actionAdmin() {


          $criteria=new CDbCriteria;

          $criteria->condition ='(protocolo.empresa='.Yii::app()->user->pessoa;

          if($_REQUEST['cliente'] != "") {

            $criteria->condition .= " AND pessoadest.nome LIKE '%".$_REQUEST['cliente']."%'";


          if ($_REQUEST['situacao'] == mensagem::SFinalizada) {

            $criteria->condition .=" and mensagem.situacao = 'Finalizada'  ";


        $pages=new CPagination(protocolo::model()->with('tpprotocolo','pessoadest','mensagem')->count($criteria));



        $sort=new CSort('protocolo');

        $sort->attributes= array(




        $sort->defaultOrder='protocolo.protocolo DESC';










as I have a primary key made up the count do not I was running the following alterations

class CJoinElement


 public function count($criteria=null){


else if (is_array($this->_table->primaryKey))





	  	foreach($this->_table->primaryKey as $name)



      $column =implode(' , ', $pk);

      $query->selects=array("COUNT(DISTINCT $column)");



brought with it the count correctly but when do the sorting or paging him ta get lost in the amount of records

would like suggestions on how to address this issue ???

For BELONGS_TO I think it’s a good idea to show up the select box containing data of the referenced table, e.g. “cliente” and then just add another criteria (with “mergeCriteria”) to existing one.

Darmen Hello dear, I’d like ta filtering both belongs_to relationship as a relationship has_one being in the relationship that my has_one fk and a composite key to making the following filters:



                                $this->widget('CAutoComplete', array(







                                'htmlOptions'=>array('class'=>campopesquisa, 'size'=>35)));



                                echo NHtml::dropDownList('situacao',$_GET['situacao'], mensagem::getSituacao(),




ie quicken filter using my relationships, the filter this way is working to put the paging and sorting not

he problem is that together() generates one query but can result in duplicate records being selected. In case I’m using CPagination class this can bring a problem when LIMIT is applied, e.g. it will result in 10 first records to be returned by database, some of them are duplicates which are then removed by Yii and as result of find() I get less then 10 records.

Any solution here to avoid problems with duplicate records using together parameter??