CGridView with searchable has_many

Hey Guys,

I have an CGridView which displays some fields like name, surname and so on. It also displays phone numbers which is another table related as HAS_MANY.

In my people model I have declared the relation:




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(

			'phones'=>array(self::HAS_MANY, 'AdPhone', 'person_id'),

		);

	}




where person_id is the index field in my ad_phone table

then I added a getter to recieve all phone numbers:





public function getAllPhoneNumbers()

	{

		// more than one phone exists for the user

		if(sizeof($this->phones) > 1)

		{

			foreach($this->phones as $phone)

			{

				echo $phone->number."<br>";

			}

		}

		else

		{

			foreach($this->phones as $phone)

			{

				return $phone->number;

			}

		}

	}




and I also added the column to my gridview:





		array('name'=>'allPhoneNumbers', 'header'=>'Phone', 'type'=>'raw', 'value'=>'$data->getAllPhoneNumbers()'),




But I have no idea how to make this field searchable like the others, the numbers are displayed properly just searching is not working with it. Could anyone assist me with this please? Thanks in advance!

-Seb

no one? :(

still looking for a solution, please let me know if someone can assist. Thanks!

you can add a virtual attribute to your model




public $virtualPhone;


// also add it to the safe attribute list of the rules array, for massive assignment

public function rules()

	{

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

		// will receive user inputs.

		return array(

			// other rules

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

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

			array('other attributes, virtualPhone', 'safe', 'on'=>'search'),

		);

	}


// then add search in your search() function

public function search()

	{

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

		// should not be searched.


		$criteria=new CDbCriteria;


                // other compares

 

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


		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}