customizing actionSearch

Hi,

I have problems with customizing autogenerated actionAdmin(). Let’s say we have 2 tables:

POST: id, author_id, title

USER: id, title (deliberately the same column as above)

I generated CRUD actions for POST and now I can search posts by id, author_id and title. But I want to be able to search posts by author.title. How can I do this?

This what I have…

POST.PHP




Post::search() {

	$criteria=new CDbCriteria;

	$criteria->compare('id',$this->id,true);

	$criteria->compare('author_id',$this->author_id,true);

	$criteria->compare('title',$this->title,true);


	return new CActiveDataProvider($this, array(

		'criteria'=>$criteria,

	));

}



ADMIN.PHP




<?php $this->widget('zii.widgets.grid.CGridView', array(

	'id'=>'post-grid',

	'dataProvider'=>$model->search(),

	'filter'=>$model,

	'columns'=>array(

		'id',

		'author_id',

		'title',

		array(

			'class'=>'CButtonColumn',

		),

	),

)); ?>



Thank you for your help. I know this is not interesting question in any way, but I am just beginning with Yii.

S.

Nobody has idea? I am trying to do it this way:

PostController.php




function PostController::actionAdmin() {

	$model=new Post('search');

	$model->unsetAttributes();

	$model->author = new User; //!!! create new user, so it is not null

	if(isset($_GET['Post']))

		$model->attributes=$_GET['Post'];

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

		'model'=>$model,

	));

}



Post.php




Post::search() {

        $criteria=new CDbCriteria;

        $criteria->compare('id',$this->id,true);

        $criteria->compare('author.title',$this->author->title,true); //!!! change author_id to author.login

        $criteria->compare('title',$this->title,true);


        return new CActiveDataProvider($this, array(

                'criteria'=>$criteria,

        ));

}



Admin.php




<?php $this->widget('zii.widgets.grid.CGridView', array(

        'id'=>'post-grid',

        'dataProvider'=>$model->search(),

        'filter'=>$model,

        'columns'=>array(

                'id',

                'author.title', //!!! change author_id to author.login

                'title',

                array(

                        'class'=>'CButtonColumn',

                ),

        ),

)); ?>



Author’s title in grid is shown ok. But this field is not searchable - there is no textfield above this column. How can I fix this?

Ok, so I finally found solution which is working for me. If someone is interested in, here it is…

Post.php




        $criteria=new CDbCriteria;

        $criteria->compare('t.id',$this->id,true);

        $criteria->compare('user.title',$this->author_id,true); //!!! change author_id to user.title

        $criteria->compare('t.title',$this->title,true);

        $criteria->with = array('user'); //!!! here we join table POST with table USER; POST has alias 't.' and USER has alias 'user.'

        $criteria->together = true;


        return new CActiveDataProvider($this, array(

                'criteria'=>$criteria,

        ));



Admin.php




<?php $this->widget('zii.widgets.grid.CGridView', array(

        'id'=>'post-grid',

        'dataProvider'=>$model->search(),

        'filter'=>$model,

        'columns'=>array(

                'id',

		array( //!!! show titles instead of ids

			'name'=>'author_id',

			'value'=>'User::model()->findByPk($data->author_id)->title',

		),

                'title',

                array(

                        'class'=>'CButtonColumn',

                ),

        ),

)); ?>