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

model:




  public function relations() {

    return array(

    'pessoadest'=>array(self::BELONGS_TO,'pessoa','cliente',

      'select'=>'nome',

      'joinType'=>'INNER JOIN',

      'alias'=>'pessoadest'

    ),

    'mensagem'=>array(self::HAS_ONE,'mensagem','protocolo,empresa',

      'together'=>True,

      'joinType'=>'INNER JOIN',

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

      'alias'=>'mensagem'

      ),

    );

  }






Controller :

  public function actionAdmin() {

        $this->processAdminCommand();

          $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));

        $pages->pageSize=self::PAGE_SIZE;

        $pages->applyLimit($criteria);

        $sort=new CSort('protocolo');

        $sort->attributes= array(

             'tpprotocolo.nome'=>'tpprotocoloNome',

             'pessoadest.nome'=>'pessoadestNome'

            );

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

        $sort->multiSort=false;

        $sort->applyOrder($criteria);

        $protocoloList=protocolo::model()->with('tpprotocolo','pessoadest','mensagem')->findAll($criteria);

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

            'protocoloList'=>$protocoloList,

            'pages'=>$pages,

            'sort'=>$sort,

        ));

    }



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))

    {

			$prefix=$this->getColumnPrefix();

			$schema=$this->_builder->getSchema();

      $pk=array();

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

					$pk[$name]=$prefix.$schema->quoteColumnName($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:




view:

   <?php

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

                                'model'=>pessoa::model(),

                                'name'=>'cliente',

                                'attribute'=>'nome',

                                'minChars'=>2,

                                'max'=>10,

                                'url'=>'index.php?r=cliente/Autocomplete',

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

                            ?>


                      <?php

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

                                array('class'=>campodropDownList,

                                'prompt'=>'Selecione'))

                            ?>



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??

Thanks

Alex