Cdbcriteria And Using Relations Table

Hi guys,

I’m stuck with an issue and i need your help, i want to use CdbCriteria with relations already defined in model related.


  public function search()

        {

                // Warning: Please modify the following code to remove attributes that

                // should not be searched.


                $criteria=new CDbCriteria;

               

                 ..... 



For example, i want to replace user_id (current model ) with username (other model )

using relation’s section ??? Is that possible ??

Post Model :

Post_Id


Post_Title


......

User Model :

user_Id


username


......

in my current model ( PostUser Model )

....


Post_Id


User_Id

I want to replace userId by username using relation "user".

If someone knows how to do, please respond me.

Thanks

Nour Eddine

Dear Friend

I hope the following link will be useful to you.

Searching and sorting by related model in CGridView

Thanks for your quick answer.

But unfortunately, the link that you gave me talks about how to use relation’s using CGridView either than using CActiveDAtaprovider or CdbCriteria.

So i keep searching of solution or waiting for another answer.

Anyway thanks friend

Hi BENZ, welcome to the forum.

Do you want to do something like this?




$criteria = new CDbCriteria();

...

$criteria->compare('user.name', $this->userName, true);

// instead of

// $criteria->compare('userId', $this->userId);

...



If you do, then I believe that the link seenivasan gave you IS the answer. :)

But, would you please try to elaborate your needs a little more?

First, thanks seenivasan and softark for the reply

I guess, i should more explain the issue :

Actually, i’m working to export data using EExcelView extension :




         $this->widget('ext.eexcelview.EExcelView', array(

            				'title' => utf8_encode("Export"),

            				'exportType'=>'Excel2007',

            				'autoWidth'=>true,           		

	        				'dataProvider' => new CActiveDataProvider(

	        				                                $data,

	        				                                array(  

                                                                  'criteria'=>$criteria,

                                                                  'pagination'=>false,                                     

	          	                                            )

	          	                               ),	          	                               	          	                               

	             ));



My table contains 4 attributes ( see attributeLabels) with 2 foreign keys (see relations )




	public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

		'collaborateur' => array (self :: BELONGS_TO,'Collaborateur','collaborateurID'),

		'typeJourConge' => array (self :: BELONGS_TO,'Typejourconge','typeJourCongeID'),		

		);

	}

  

	/**

	  * @return array customized attribute labels (name=>label)

	 */

	public function attributeLabels()

	{

		return array ( 

			'DateJourCongeF' => 'DateJourCongeF',

			'DateJourCongeD' => 'DateJourCongeD',

			'collaborateurID' => 'collaborateur',

			'typeJourCongeID' => 'typeJourConge',		

		);

	}



But when i open the generated file, collaborateurID and typeJourCongeID contains numbers ( foreign key ), But what i’m trying to do since 3h, is to get collaborateurName in place of collaborateurID using relation collaborateur.

I hope, i was clear :)

I see. :)

Try something like the following




$criteria = new CDbCriteria();

$criteria->with = array(

    'collaborateur',

    'typeJourConge',

);

$criteria->select = array(

    't.DateJourCongeF',

    't.DateJourCongeD',

    'collaborateur.collaborateurName',

    'typeJourConge.typeJourCongeName',

);

...