Yii Framework Forum: DropDownList >> multiple - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

DropDownList >> multiple Populated with previously selected values in update view Rate Topic: -----

#1 User is offline   Backslider 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 368
  • Joined: 23-July 09

Posted 06 December 2010 - 12:06 AM

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?
We were all once expert at....... nothing.

yii-language-behavior

My Blog
0

#2 User is offline   Backslider 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 368
  • Joined: 23-July 09

Posted 06 December 2010 - 10:10 AM

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 :)
We were all once expert at....... nothing.

yii-language-behavior

My Blog
0

#3 User is offline   Backslider 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 368
  • Joined: 23-July 09

Posted 07 December 2010 - 05:07 PM

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;
    }

We were all once expert at....... nothing.

yii-language-behavior

My Blog
0

#4 User is offline   Igor Ivanovic 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 211
  • Joined: 17-October 10
  • Location:Zagreb

Posted 07 December 2010 - 05:53 PM

View PostBackslider, on 06 December 2010 - 10:10 AM, said:

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')); ?>


0

#5 User is offline   Backslider 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 368
  • Joined: 23-July 09

Posted 07 December 2010 - 10:56 PM

View PostIgor Ivanovic, on 07 December 2010 - 05:53 PM, said:

First at all why you use a param in query when param is defined variable and you cant change it? :)


What makes you think I cannot change it??

    if(isset($_GET['lang'])) {
      Yii::app()->params['lang'] = (int)$_GET['lang'];
    }

We were all once expert at....... nothing.

yii-language-behavior

My Blog
0

#6 User is offline   Igor Ivanovic 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 211
  • Joined: 17-October 10
  • Location:Zagreb

Posted 08 December 2010 - 04:56 AM

View PostBackslider, on 07 December 2010 - 10:56 PM, said:

What makes you think I cannot change it??

    if(isset($_GET['lang'])) {
      Yii::app()->params['lang'] = (int)$_GET['lang'];
    }


Sory i dint konw that thanks for your advice :)
0

#7 User is offline   Backslider 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 368
  • Joined: 23-July 09

Posted 08 December 2010 - 10:17 AM

View PostIgor Ivanovic, on 08 December 2010 - 04:56 AM, said:

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');

We were all once expert at....... nothing.

yii-language-behavior

My Blog
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users