February 12, 2007

A Picklist Component for CakePHP

CakePHP — Sarah King

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:
  1. CREATE TABLE `picklists` (<br />
  2. `id` int(11) NOT NULL auto_increment,<br />
  3. `listname` varchar(20) NOT NULL,<br />
  4. `item` varchar(50) NOT NULL,<br />
  5. `image` varchar(50) default NULL,<br />
  6. `defaultitem` int(3) NOT NULL default '0',<br />
  7. `sortorder` int(4) NOT NULL default '10',<br />
  8. PRIMARY KEY  (`id`)<br />
  9. ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COMMENT='to hold picklist information for any form' AUTO_INCREMENT=4 ;</p>
  10. <p>INSERT INTO `picklists` VALUES (1, 'regions', 'Northland', '', 0, 1);<br />
  11. INSERT INTO `picklists` VALUES (2, 'regions', 'Waikato', '', 0, 3);<br />
  12. INSERT INTO `picklists` VALUES (3, 'regions', 'Auckland', '', 1, 2);

and the simplest of models.

PHP:
  1. class Picklist extends AppModel {<br />
  2. var $name = 'Picklist';<br />
  3. var $validate = array();<br />
  4. }

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:
  1. class PicklistComponent extends Object<br />
  2. {<br />
  3. var $controller = true;<br />
  4. var $Picklist = null;</p>
  5. <p>function startup(&$controller)<br />
  6. {<br />
  7. // This method takes a reference to the controller which is loading it.<br />
  8. // Perform controller initialization here.<br />
  9. $this->controller = $controller;<br />
  10. loadModel('Picklist');<br />
  11. $this->Picklist = new Picklist;<br />
  12. }</p>
  13. <p>function getlist($listname)<br />
  14. {<br />
  15. $picklist = new Picklist;<br />
  16. $output = $this->Picklist->generateList(<br />
  17. array('`Picklist`.`listname`' => $listname),<br />
  18. '`sortorder`, `item`',<br />
  19. null,<br />
  20. '{n}.Picklist.id',<br />
  21. '{n}.Picklist.item'<br />
  22. );<br />
  23. return $output;<br />
  24. }//getlist</p>
  25. <p>function getfulllist($listname)<br />
  26. {</p>
  27. <p>$output = $this->Picklist->findAll( array('`Picklist`.`listname`' => $listname),<br />
  28. null,<br />
  29. '`Picklist`.`sortorder`, `Picklist`.`item`'<br />
  30. );<br />
  31. return $output;<br />
  32. }//getlist<br />
  33. }

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.

Also on this site

Tags:

No Comments »

No comments yet.

Leave a comment

RSS feed for comments on this post. TrackBack URI

Search

Pages

Categories:

Other Resources

Subscribe in NewsGator Online
GeoURL