CakePHP HABTM Select box in edit mode not populating

103 views Asked by At

I feel like I'm going insane. Usually I can figure these things out but this one has be truly stuck. I'm using Cake 2.3.7. I noticed there is a bug in earlier versions of CakePHP.

Hope someone can point out where I'm going wrong. On my site I have Articles that have 3 HABTM associations > Flies, Rivers and Lakes. So:

Article - HABTM - Fly Article - HABTM - River Article - HABTM - Lake

Controller:

...
$flies = $this->Article->Fly->find('list');
$lakes = $this->Article->Lake->find('list');
$rivers = $this->Article->River->find('list');
$this->set(compact('flies', 'lakes', 'rivers'));

View:

echo $this->Form->input('Fly');
echo $this->Form->input('Lake');
echo $this->Form->input('River');

This syntax seems to be exactly as described in the manuals for CakePHP 2.x, and it works on my local test system, but as soon as I upload it to my server, it fails.

Any help with this would be greatly appreciated, and let me know if more information is needed.

3

There are 3 answers

1
Royalty On

Why don't you feed the input options? Like so

echo $this->Form->input('Fly',array('options'=>$flies));
echo $this->Form->input('Lake',array('options'=>$lakes));
echo $this->Form->input('River',array('options'=>$rivers));
1
drmonkeyninja On

For HABTM associations your form fields should be:-

echo $this->Form->input('Fly.Fly');
echo $this->Form->input('Lake.Lake');
echo $this->Form->input('River.River');

Otherwise what you're doing in the controller looks correct.

0
Keith Lugthart On

The server I was uploading to, somehow had a copy of CakePHP 2.3.0 and the app was pointing to it. CakePHP has a known bug that causes this.

Thanks to all who replied.