Hi, there.
$form_option_helper->options_from_collection_for_select() can't work when I set 'id' the second parameter ($value_column_name).
For example, this code (in a view file) shows a blank page.
<select name="food_id">
<option value=""></option>
<?php echo $form_options_helper->options_from_collection_for_select(
$Food->find(),
'id',
'name',
$params['food_id'] ? $params['food_id'] : ''
); ?>
</select>
So, I edited lib/views/helpers/form_options_helper.php and fixed it.
lib/views/helpers/form_options_helper.php line 194 -
Before:
194 $name = method_exists($item,$text_column_name) ? $item->$text_column_name() : $item->get($text_column_name);
195 $collection_options[$name] = method_exists($item,$value_column_name) ? $item->$value_column_name() : $item->get($value_column_name);
After:
194 $name = method_exists($item,$text_column_name) ? $item->$text_column_name() : $item->get($text_column_name, false);
195 $collection_options[$name] = method_exists($item,$value_column_name) ? $item->$value_column_name() : $item->get($value_column_name, false);
But I don't know whehere this solution is correct or not about a correct usage of this helper method.
Is this modification correct?
get(attribute,false) is preventing to get the attribute from ->getId(), instead it uses $this->id.
That's usually not what you want. Do you have modified ->getId() or getPrimaryKey()? Do you have a very large collection you loop over?
my unit_test for this is:
function testT()
{
list($Food,) = $this->useModel('Food=>id,name');
$n = 200;
for ($i = 0;$i < $n;$i++){
$this->createFood("name: Frucht$i");
}
for ($i = 0;$i < ($n/5);$i++){
$selected[] = rand(1,$n);
}
$Form = new FormOptionsHelper();
$form_string = $Form->options_from_collection_for_select(
$Food->find('all'),'id','name',$selected
);
var_dump($form_string);
}
creates 200 items, selects a fifth of them randomly.
1 to 2 of 2