Select box in CakePHP 1.2 (generateList method alternative for CakePHP 1.2)

Method 'generateList' used in CakePHP 1.1 model, now has been removed from the latest version of CakePHP. But still we can get the same functionality by just doing a little change in the app_model.php file in the cake libs.

Go to the file cake/libs/model/app_model.php. Add the following method in the class AppModel:


function getlist ($cond=null,$order=null,$limit=null,$key=null,$val=null) {
return $this->find("list",array(
'conditions' => $cond,
'order' => $order,
'limit' => $limit,
'fields' => array(str_replace('{n}.','',$key), str_replace('{n}.','',$val))
));
}

Now, we can use this getlist method from controller. Suppose we need the array of product ids and their respective name, for generating a product drop down:


$products = $this->Product->getlist(null, null, null, 'Product.id', 'Product.name');

This will return the required array. Now we can use this array in the .ctp file for generating the select/dropdown box:


echo $form->select('Product.product_id', $products, null, array('label'=>false, 'div'=>false), 'Select One');