Create Custom Side Menus
Frodo's Ghost | Cakephp : Form Options List Variable
post-template-default,single,single-post,postid-46,single-format-standard,eltd-core-1.0.1,ajax_fade,page_not_loaded,,borderland - frodosghost-child-ver-1.0.0,borderland-ver-1.2, vertical_menu_with_scroll,smooth_scroll,side_menu_slide_with_content,width_470,paspartu_enabled,paspartu_on_bottom_fixed,wpb-js-composer js-comp-ver-4.4.4,vc_responsive

Cakephp : Form Options List Variable

So, it seems that cake is more awesome than me. And the Temporary hack I have below is over-ruled by cakephp. Thanks to Matt @ pseudocoder what I should have looked for is find $this->Model->(‘list’) and it would bypass the ‘foreach’ loop. Excellent.

The CakePHP Way

Many thanks to Matt in the comment below, he pointed me to the real Cake way to do this. So, ignore my tmporary hack. It’s easy.

[php]function add(){
$UserOutposts = $this->Arbiter->find(‘list’, array(
‘conditions’ => array(‘(Arbiter.status & ? = ?)’ => array(Arbiter::USER, Arbiter::USER)),
‘contain’ => false,
‘fields’ => array(‘’)

$this->set(‘outposts’, $UserOutposts);

The Old Way (Temp Hack Over-ruled)

One of the reasons I love CakePHP is its ease of use. Once you speak its language everything flows together like a river.

Sometimes though I have fallen into hacking cake to make it do what I’d like. It used to be because I didn’t know it enough. Sometimes now it is because I need specifics in my views. And sometimes it was because I wanted a html options box, but cake didn’t have the data in the format I wanted it.

So this is my hack I did today.

It seems to be the one thing with the FormHelper that got me a little stuck. I was trying to set up a options box. The way we have been doing this is looping a ‘real box’ out in the view – using the cake returned arrays and then using ‘foreach’ to loop out the data we needed.

I wasn’t really a fan of that idea, so I had a go at hacking together a mid point. I’m sure someone will know a much better ‘cake’ way of doing this – and if you do, please leave a comment. But here is what I am calling a temporary hack (basically it make my cake mind feel like I haven’t wronged cakephp).

Temporary Hack : FormHelper Options Box in view

I am going to send the view a $var with a key-value paired array. So I find a list of all the Arbiters (a master list). That have a status of USER (see Mark Story’s Bitmasks). The ‘contain’ will return just the Arbiter data, nothing related. And return just the fields I need for the loop.

Then I just use a ‘foreach’ loop to push the found fields into the $var array I had setup. And using $this->set it goes to the view.

[php]//Outposts Controller
function add(){
$UserOutposts = $this->Arbiter->find(‘all’, array(
‘conditions’ => array(‘(Arbiter.status & ? = ?)’ => array(Arbiter::USER, Arbiter::USER)),
‘contain’ => false,
‘fields’ => array(‘id’,’name’)

$opList = array();
foreach($UserOutposts as $UO){
$opList[$UO[‘Arbiter’][‘id’]] = $UO[‘Arbiter’][‘name’];

$this->set(‘outposts’, $things);

That way in the view:

[php]// views/outposts/add.ctp
echo $form->input(‘field’, array(‘options’ => $outposts, ’empty’ => ‘(choose one)’));

Just put the sent var into the options and your good to go with an Options box, without having to build or loop it in the view.

As I said, I reckon this is a hack:

  • a) Because its in the controller and the rule is Fat Models -> Skinny Controllers
  • b) It still doesn’t seem as easy as cake should be. I have a rule with cakephp, if its taking too long it means I’m doing it wrong.

So I am waiting for someone to correct me. But until then I do have this temporary hack kicking out the jams.

  • Check out find(‘list’) –“ rel=”nofollow”>

    February 24, 2009 at 2:08 pm
  • $UserOutposts = $this->Arbiter->find(‘list’, array(
    ‘conditions’ => array(‘(Arbiter.status & ? = ?)’ => array(Arbiter::USER, Arbiter::USER)),
    ‘contain’ => false,
    ‘fields’ => array(‘id’,’name’)

    That should work, strait up!

    February 25, 2009 at 6:38 am
  • Done and Done.
    @Matt Curry hit me up earlier with the hint, and got it happening right away.

    You can even just have 'fields' => array('name') : The id is auto with just that data. I even got it working with 'fields' => array('name','name') : The name for the Value and for the Key. Nice.

    Thanks for the code, by the way.

    February 25, 2009 at 11:28 am

Post a Comment