I needed to have a table holding all my picklists, to make them easier to edit and manage. It’s a large application so there will be lots of drop downs and radio buttons, states and status fields.
I started with my picklist table
[code]CREATE TABLE `picklists` (
`id` int(11) NOT NULL auto_increment,
`listname` varchar(20) NOT NULL,
`item` varchar(50) NOT NULL,
`image` varchar(50) default NULL,
`defaultitem` int(3) NOT NULL default ‘0’,
`sortorder` int(4) NOT NULL default ’10’,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COMMENT=’to hold picklist information for any form’ AUTO_INCREMENT=4 ;
INSERT INTO `picklists` VALUES (1, ‘regions’, ‘Northland’, ”, 0, 1);
INSERT INTO `picklists` VALUES (2, ‘regions’, ‘Waikato’, ”, 0, 3);
INSERT INTO `picklists` VALUES (3, ‘regions’, ‘Auckland’, ”, 1, 2);[/code]
and the simplest of models.
[php]class Picklist extends AppModel {
var $name = ‘Picklist’;
var $validate = array();
}[/php]
The absense of hasMany of belongsTo is for performance reasons. I don’t want cakePHP getting bogged down building huge arrays everytime it hits a reference to my picklist table.
And finally the component
[php]class PicklistComponent extends Object
{
var $controller = true;
var $Picklist = null;
function startup(&$controller)
{
// This method takes a reference to the controller which is loading it.
// Perform controller initialization here.
$this->controller = $controller;
loadModel(‘Picklist’);
$this->Picklist = new Picklist;
}
function getlist($listname)
{
$picklist = new Picklist;
$output = $this->Picklist->generateList(
array(‘`Picklist`.`listname`’ => $listname),
‘`sortorder`, `item`’,
null,
‘{n}.Picklist.id’,
‘{n}.Picklist.item’
);
return $output;
}//getlist
function getfulllist($listname)
{
$output = $this->Picklist->findAll( array(‘`Picklist`.`listname`’ => $listname),
null,
‘`Picklist`.`sortorder`, `Picklist`.`item`’
);
return $output;
}//getlist
}[/php]I don’t use a controller because there is no callable interface to my picklists from this application. I’ll be building an independant admin application to maintain the list – more on that later.
I started out with a picklist controller and used $this->requestAction(‘/picklists/getlist/reportproblem’) to return the list. However on reading more I’ve learnt that the component is more appropriate.
Be First to Comment