I have a dropdown list:
<?php echo $form->dropDownList($cats, 'category_id', CHtml::listData(Categories::model()->getCats(),'category_id','category_name'), array('prompt'=> 'Please Select', 'multiple' => 'multiple')); ?>
I can populate the $cats variable like this and all is fine:
$criteria=new CDbCriteria;
$criteria->condition = 'language_id = ' . Yii::app()->params['lang'] . ' and product_id = ' . $_GET['id'];
$cats = ProductCategories::Model()->find($criteria);
This will return the dropdown list in update view with a selected category. Problem is that this is a multiple select list, but if I try the following I get an error:
$cats = ProductCategories::Model()->findAll($criteria);
findAll() returns an array of objects and gives the following error:
get_class() expects parameter 1 to be object, array given
So, how can I ouput this dropdownlist with multiple options selected?
Ok, solved this in a roundabout way, but suspect there is an easier way.
Firstly, I changed my dropdown list:
<?php echo CHtml::dropDownList('ProductCategories[category_id[]]', $cats, CHtml::listData(Categories::model()->getCats(),'category_id','category_name'), array('prompt'=> 'Please Select', 'multiple' => 'multiple')); ?>
I then populated the $cats variable (previously selected options in database records) as follows:
$criteria=new CDbCriteria;
$criteria->condition = 'language_id = ' . Yii::app()->params['lang'] . ' and product_id = ' . $id;
$cat_result = ProductCategories::Model()->findAll($criteria);
$cats = array();
foreach($cat_result as $val) {
$cats[] = $val->category_id;
}
If somebody knows a more “correct” way, please post
Backslider
(Developer)
December 7, 2010, 10:07pm
3
A more correct way to populate my $cats variable, but have not found how to get around the fact that Yii returns relations as an array of objects.
Since my product_categories table is declared in relations, I can use the following:
$cats = array();
foreach($model->productCategories as $val) {
$cats[] = $val->category_id;
}
iivano71
(Igor Zg1987)
December 7, 2010, 10:53pm
4
Backslider:
Ok, solved this in a roundabout way, but suspect there is an easier way.
Firstly, I changed my dropdown list:
<?php echo CHtml::dropDownList('ProductCategories[category_id[]]', $cats, CHtml::listData(Categories::model()->getCats(),'category_id','category_name'), array('prompt'=> 'Please Select', 'multiple' => 'multiple')); ?>
I then populated the $cats variable (previously selected options in database records) as follows:
$criteria=new CDbCriteria;
$criteria->condition = 'language_id = ' . Yii::app()->params['lang'] . ' and product_id = ' . $id;
$cat_result = ProductCategories::Model()->findAll($criteria);
$cats = array();
foreach($cat_result as $val) {
$cats[] = $val->category_id;
}
If somebody knows a more “correct” way, please post
First at all why you use a param in query when param is defined variable and you cant change it?
$criteria=new CDbCriteria;
$criteria->condition = 'language_id = ' . Yii::app()->params['lang'] . ' and product_id = ' . $id;
$cat_result = ProductCategories::Model()->findAll($criteria);
$data =CHtml::listData($cat_result,'id','ime');
and in view
<?php echo CHtml::dropDownList('ProductCategories[category_id][]', 0, $data, array('prompt'=> 'Please Select', 'multiple' => 'multiple')); ?>
What makes you think I cannot change it??
if(isset($_GET['lang'])) {
Yii::app()->params['lang'] = (int)$_GET['lang'];
}
iivano71
(Igor Zg1987)
December 8, 2010, 9:56am
6
Sory i dint konw that thanks for your advice
To me its the easiest way when working with a multilingual site in Yii. I use an integer for language_id to keep it simple, since to filter the input (URL) all that is needed is (int)… rather than lang=es which opens the application to attack and must be filtered.
And thanks to you for reminding me I can use listData:
$cats = CHtml::listData($model->productCategories,'category_id', 'category_id');