2

I have 2 attributes in model jobs, they are tag and category, I want to make a dropDownList that display the value of tag who have category equal 'salary', here is my code:

$s= CHtml::listData($model2, 'salary', 'tag');                         
echo CHtml::dropDownList('salary', 'salary', $s);

my db:

tag / category

1 / val1

2 / val1

a / val2

1000 / salary

2000 / salary

but I got a dropDownList that contains only the last value who have these conditions. what is the wrong in my code?

Mohammad
  • 3,449
  • 6
  • 48
  • 75

1 Answers1

1

There are two reasons why you can get only one results, firstly the number of results you get in $model2 affects the results, check if your are using findAll to return the all the rows matching a condition and not find and findByPk as they return only one value,

Secondly the second attribute of listData should be your value field 'salary' is not an attribute of the model, this has to be the valueField(see this).

It will remain same if it assigned as salary - a constant value for all option elements, meaning your array will be overwritten for each reach leaving you with the array of one element containing the last value.

You should 'tag_id'/'id' or some sort of primary key from your model or tag value( assuming it is unique) to identify the tag by

$model2 = MyModel::model()->findAll("category = salary");
$s= CHtml::listData($model2, 'tag', 'tag');                         
echo CHtml::dropDownList('salary', 'salary', $s,array('empty'=>'--Select--'));
Manquer
  • 7,390
  • 8
  • 42
  • 69
  • but `$model2` will be changed everytime due to the client conditions, at the first time `$model2` is initialized to `findAll()`, then it will be changed – Mohammad Jun 24 '14 at 10:06
  • hey see my revised answer, your problem is different than what i thgt, if you use already use findAll – Manquer Jun 24 '14 at 10:14
  • I have changed `salary` to `category` which is the name of the model attribute that contain the value `salary`, the result is: I got for each value of `category` the last value of `tag` that match the `categoty`, i will show you my db in the question – Mohammad Jun 24 '14 at 10:21
  • It seems you don't have some sort of unique key , you can instead use the value of tag itself twice, as i have done now in my updated answer. See if that helps – Manquer Jun 24 '14 at 10:42
  • this will return all the values of `tag` – Mohammad Jun 24 '14 at 10:58
  • your $model2 is where your data is found, all the records that satisfy model2 findAll condition will only show – Manquer Jun 24 '14 at 11:13
  • ok now I added a column to db `idSalary`, it will put an id for each tag have cateogry salary, and the others will be null, when I used it instead of `salary` as a valueField, it returns all salaries and a non-salary value, at first, so the results are : united states, 1000$, 2000$, 3000$ ...why united states is appeared ?? – Mohammad Jun 24 '14 at 11:20
  • most likely it is the placeholder/empty value, you can configure it in the fourth element of your `dropDownList` like this `CHtml::dropDownList('salary', 'salary', $s,array('empty'=>'--Select--'));` – Manquer Jun 24 '14 at 11:23
  • the placeholder is exist – Mohammad Jun 24 '14 at 11:43