Activetextfield And Relations

Hello,

In the following link I found a solution for my problem which doesn’t work.

http://www.yiiframework.com/forum/index.php/topic/2765-questions-regarding-relations-w-activetextfields/

If I follow the solution, the page will partially render and then will stop. The rendered result is minimal. No graphics, just text.

I have exactly the same problem like the one from the link.

Here’s a section of my code:

AlbumController




public function actionAdmin()

	{

		$model=new Album('search');

		$model->unsetAttributes();  // clear any default values

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

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

		

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

			'model'=>$model,

		));

	}



admin.php View




<?php

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

	'id'=>'album-grid',

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

	'filter'=>$model,

	'columns'=>array(

		...

		array(

				'header'=>'Category Name',

				'name'=>'category.name',

				'filter'=>CHtml::activeTextField($model,"name"),

		),

		...

	),

)); ?>



The problem occurs at this line


'filter'=>CHtml::activeTextField($model,"name"),

I have tried all possibilities and combinations but it doesn’t work. I also tried the solution from the link provided above but I get the same results everytime.

LE: I forgot to post models code

Album Model




<?php


/**

 * This is the model class for table "album".

 *

 * The followings are the available columns in table 'album':

 * @property integer $id

 * @property string $name

 * @property string $description

 * @property integer $cover_pitcure_id

 * @property integer $category_id

 *

 * The followings are the available model relations:

 * @property Category $category

 * @property Picture[] $pictures

 */

class Album extends CActiveRecord

{

	/**

	 * Returns the static model of the specified AR class.

	 * @return Album the static model class

	 */

	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}


	/**

	 * @return string the associated database table name

	 */

	public function tableName()

	{

		return 'album';

	}


	/**

	 * @return array validation rules for model attributes.

	 */

	public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('name, category_id', 'required'),

			array('cover_pitcure_id, category_id', 'numerical', 'integerOnly'=>true),

			array('name', 'length', 'max'=>30),

			array('description', 'safe'),

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('id, name, description, cover_pitcure_id, category_id, category.id, category.name', 'safe', 'on'=>'search'),

		);

	}


	/**

	 * @return array relational rules.

	 */

	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(

			'category' => array(self::BELONGS_TO, 'Category', 'category_id'),

			'pictures' => array(self::HAS_MANY, 'Picture', 'album_id'),

		);

	}


	/**

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

	 */

	public function attributeLabels()

	{

		return array(

			'id' => 'ID',

			'name' => 'Name',

			'description' => 'Description',

			'cover_pitcure_id' => 'Cover Pitcure',

			'category_id' => 'Category',

		);

	}


	/**

	 * Retrieves a list of models based on the current search/filter conditions.

	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.

	 */

	public function search()

	{

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

		// should not be searched.


		$criteria=new CDbCriteria;


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

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

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

		$criteria->compare('category_id',$this->category_id);

		//$criteria->compare('category.name',$this->category);

		


		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}

}



Category Model


<?php


/**

 * This is the model class for table "category".

 *

 * The followings are the available columns in table 'category':

 * @property integer $id

 * @property string $name

 *

 * The followings are the available model relations:

 * @property Album[] $albums

 */

class Category extends CActiveRecord

{

	/**

	 * Returns the static model of the specified AR class.

	 * @return Category the static model class

	 */

	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}


	/**

	 * @return string the associated database table name

	 */

	public function tableName()

	{

		return 'category';

	}


	/**

	 * @return array validation rules for model attributes.

	 */

	public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('name', 'required'),

			array('name', 'length', 'max'=>30),

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('id, name', 'safe', 'on'=>'search'),

		);

	}


	/**

	 * @return array relational rules.

	 */

	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(

			'albums' => array(self::HAS_MANY, 'Album', 'category_id'),

		);

	}


	/**

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

	 */

	public function attributeLabels()

	{

		return array(

			'id' => 'ID',

			'name' => 'Name',

		);

	}


	/**

	 * Retrieves a list of models based on the current search/filter conditions.

	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.

	 */

	public function search()

	{

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

		// should not be searched.


		$criteria=new CDbCriteria;


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

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


		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}

}

Take a look at this classic: http://www.yiiframework.com/wiki/281/searching-and-sorting-by-related-model-in-cgridview/

The issue still persists because of this method


public static string activeTextField(CModel $model, string $attribute, array $htmlOptions=array ( ))

I must provide the first parameter as a model and the second as a string. By using your link I will not obtain a model to pass to activeTextField (or I could obtain but I wouldn’t be able to find what I was looking for).

If I understood what you want to achieve, it’s a normal filter box (textfield) under the column header, right? And we’re speaking of an attribute of one of the model’s relations, right? If both my assumptions are right, you don’t need the filter stuff (that causes your problem) and you’ll enjoy following the wiki.