hammies
hammies

Reputation: 1435

CHtml::listData findall returns last element

I have a dropdown list. Within the dropdownlist, I use listdata to retrieve the data from another table. But strangely, it only gets the last item in the table.

$form->dropDownList($model,'status_id',CHtml::listData(OrderStatus::model()->findAll(),'status_id', 'status'))

chtml::listdata strangely only shows this array(1) { [""]=> string(9) "Delivered" } while in the table there are 7 rows/id, where delivered is the last entry. What happened to the others?

Another odd thing is that $model->status_id is actually id 1, so it shouldn't display 'Delivered', it should be showing 'New'.

Upvotes: 1

Views: 776

Answers (3)

ajitpawarink
ajitpawarink

Reputation: 444

Your chtml::listdata strangely showing array(1) { [""]=> string(9) "Delivered" }

Is because you must have same entries in status_id column of all the rows in OrderStatus which is blank/null as per above array.

In below call

CHtml::listData(OrderStatus::model()->findAll(),'status_id', 'status')

status_id is key( index of array ) for your generated list array & its getting overwritten by same value everytime, thats whay its showing only one & last value.

Upvotes: 0

Mihai P.
Mihai P.

Reputation: 9357

See if you have by any chance a default scope.

Just do a debug of OrderStatus::model()->findAll() and see if it returns 7 records or just 1.

Upvotes: 0

Hiten Naresh Vasnani
Hiten Naresh Vasnani

Reputation: 538

Take a look at this:

Example 1: Generating a list data for categories

    // you can use here any find method you think proper to return your data from db*/

    $models = categories::model()->findAll();

    // format models resulting using listData     
    $list = CHtml::listData($models, 'category_id', 'category_name');    
    print_r($list);

HTML Output (Example):

    array("1" => "Arts", "2" => "Science", "3" => "Culture");

Upvotes: 1

Related Questions