Filter with HAS_MANY

Good morning everyone, I have a problem there is a good time, I tried several existing threads on the forum and nothing. In the listing of news, I would like to list in this column and pageslot pageslot, wanted to put a combo to filter populated with all pageslot. There were many unsuccessful attempts, here’s one:

News.php model, method, relations


'newsPageSlot' => array(self::MANY_MANY, 'NewsPageSlot', 'newsPublications(news_id, pageslot_id)'),

			'newsPublications' => array(self::HAS_MANY, 'NewsPublication', 'news_id'),

search method


$criteria->with = array('user', 'segmentation', 'newsPublications');

...

$criteria->compare('pageslot_id', NewsPublication::model()->getPublicationNews( $this->id )->pageslot_id);

The problem is that the combo does not appear in the column filter pageslot (image 1)

Follow my modeling (Fig. 2)

Thank you!

Based on this, did this:


'newsPublications' => array(self::HAS_MANY, 'NewsPublication', 'pageslot_id'),

			//'newsPageSlots'=>array(self::HAS_MANY,'NewsPageSlot','pageslot_id','through'=>'newsPublications'),

and nothing appears in the filter grid.

How do you configure your CGridView in your view?


...

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

	'id'=>'news-grid',

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

	'filter'=>$model,

	'columns'=>array(

...

array(

			'name' => 'NewsPublication.pageslot_id',

			'value' => 'NewsPublication::model()->getPublicationNews( $data->id )->pageslot->name',

			'filter' => CHtml::listData(NewsPageSlot::model()->findAll(),'id','name'),

		),

...

CHtml::listData() returns an array, but you need some html code for filter. Try this:




'filter' => CHtml::activeDropDownList(

	$model,

	'pageslot_id',

	CHtml::listData(NewsPageSlot::model()->findAll(), 'id', 'name')

),



It worked, thank you, but odd, because in another column this function:




array(

        	'name' => 'segmentation_id',

            'value' => '$data->segmentation->name',

            'filter' => CHtml::listData(Segmentation::model()->findAll(),'id','name'),

		),



It must be because of the relationship, which is my biggest problem here has segmentation_id news, however is pageslot_id news_publication where news_publication has news_id.

Well, the combo came out, but now the problem is the relationship mentioned in the first post, as would be the model according to the News DER I posted?

Thanks for listening friends!

according to documentation in the model $News, did this way:




public function relations()

	{

...

return array(

...

'newsPublications' => array(self::HAS_MANY, 'NewsPublication', 'news_id'),

			'newsPageSlots'=>array(self::HAS_MANY,'NewsPageSlot','pageslot_id', 'through'=>'newsPublications'),

..


public function search()

	{

...

$criteria=new CDbCriteria;

		$criteria->with = array('user', 'segmentation', 'newsPublications', 'status', 'newsPageSlots');

...

$criteria->compare('pageslot_id', NewsPublication::model()->getPublicationNews( $this->id )->pageslot_id);

...



But when selecting an option from the combo, it sends the value, but the combo back with the initial value (empty) and not apply the filter on the grid.

I tried several ways but does not include the table in the query news_publication.




...

public function relations()

	{

return array(

...

'newsPublications' => array(self::HAS_MANY, 'NewsPublication', 'news_id'),

			'newsPageSlots'=>array(self::HAS_MANY,'NewsPageSlot','pageslot_id', 'through'=>'newsPublications'),

		);

	}

...



I put the value manually for testing:




...

public function search()

	{

...

$criteria=new CDbCriteria;

		$criteria->with = array('user', 'segmentation', 'newsPublications', 'status', 'newsPageSlots');

...

$criteria->compare('newsPublications.pageslot_id', 12);

...



Hey, it worked, but now I do not know how to send the value for the model selected in the filter. When I select a filter value in the combo loses value:

view


array(

			'name' => 'slotName',

			'filter' => CHtml::activeDropDownList(NewsPublication::model(), 'pageslot_id', CHtml::listData(NewsPageSlot::model()->findAll(), 'id', 'name'), array('prompt' => ''))

		),

model:




public $slotName;

public function relations()

	{

		return array(

			'newsPublications' => array(self::HAS_MANY, 'NewsPublication', 'news_id'),

			'newsPageSlots'=>array(self::HAS_MANY,'NewsPageSlot','pageslot_id', 'through'=>'newsPublications'),

		);

	}


public function search()

	{

		$criteria=new CDbCriteria;

		$criteria->with = array('newsPageSlots');

		# utiliza through no relacionamento 

		$criteria->together = true;

		$criteria->compare('newsPageSlots.name', $this->slotName);

		

		return new CActiveDataProvider(get_class($this), array(

			'criteria'=>$criteria,

		));

	}