isla
(Swisscom90)
1
Im trying to make a search dropdown which search for different table-columns depending on what you choose in the dropdown.
The first choice show all rows with category_work_id = 1,2,3
the second choice show all rows with category_work_id = 4,5,6
the third choice show all rows with category_portfolio_id = 1,2,3
the fourth choice show all rows with category_portfolio_id = 4,5,6
all in the same table.
Any ideas? 
softark
(Softark)
2
Hi isla, welcome to the forum.
- Add a virtual attribute to your model and declare it safe on search
public $searchChoice;
...
public function rules()
{
return array(
...
array('searchChoice, ...', 'safe', 'on'=>'search'),
);
}
- Declare search choice options and their texts in your model.
const CHOICE_ONE = 1;
const CHOICE_TWO = 2;
const CHOICE_THREE = 3;
const CHOICE_FOUR = 4;
public static $searchChoiceOptions = array(
self::CHOICE_ONE => 'Work = 1,2,3',
self::CHOICE_TWO => 'Work = 4,5,6',
self::CHOICE_THREE => 'Portofolio = 1,2,3',
self::CHOICE_FOUR => 'Portofolio = 4,5,6',
);
- Use the virtual attribute in your search form.
echo $form->dropDownList($model, 'searchChoice', YourModel::$searchChoiceOptions, $options);
- Construct the suitable criteria depending on the given searchChoice in your search() function.
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare(...);
...
if ($this->searchChoice == self::CHOICE_ONE)
{
$criteria->addInCondition(...);
}
else if ($this->searchChoice == self::CHOICE_TWO)
{
$criteria->addInCondition(...);
}
...
return new CActiveDataProvider(get_class($this), array(
'criteria' => $criteria,
));
}
[EDIT]
I failed to add ‘static’ keyword to $searchChoiceOptions in the original post … it was a bonehead.
isla
(Swisscom90)
3
When I make the changes and load the page it only loads 1/10 of the page, not including the layout or the data.
What does the $options variable do in the search-view?
echo $form->dropDownList($model, 'searchChoice', Swimmer::$searchChoiceOptions, $options);
softark
(Softark)
4
You should have some error in the view file.
Maybe unmatched tags … missing closing/opening tags.
It is ‘htmlOptions’ of CActiveForm::dropDownList().
Look it up in the reference.
http://www.yiiframework.com/doc/api/1.1/CActiveForm/#dropDownList-detail
If you don’t want to set any options, you can omit it:
echo $form->dropDownList($model, 'searchChoice', Swimmer::$searchChoiceOptions);
isla
(Swisscom90)
5
I can’t find the problem in the view file, it must be in the model then i guess.
This is my _search view
<?php $form=$this->beginWidget('CActiveForm', array(
'action'=>Yii::app()->createUrl($this->route),
'method'=>'get',
)); ?>
<div class="row">
<?php echo $form->dropDownList($model, 'searchChoice', Swimmer::$searchChoiceOptions); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Search'); ?>
</div>
<?php $this->endWidget(); ?>
Should the constants and the searchChoiceOptions array be included in a function?
softark
(Softark)
6
No, the constants and $searchChoiceOptions are to be declared in the model class.
I don’t see any problem in your _search view, either.
I guess your _search view is included in your main view by renderPartial, isn’t it?
What about the main view? Isn’t there any mismatching tag?
[EDIT]
Ah, sorry. I forgot to add the ‘static’ keyword. 
public static $searchChoiceOptions = array(
self::CHOICE_ONE => 'Work = 1,2,3',
self::CHOICE_TWO => 'Work = 4,5,6',
self::CHOICE_THREE => 'Portofolio = 1,2,3',
self::CHOICE_FOUR => 'Portofolio = 4,5,6',
);