开发者

AutoMagic select box not populating in CakePHP

I've got the following relationship set-up between two models

  • Story belongsTo StoryType
  • StoryType hasMany Story

I've set-up a form to select the StoryType for each story using the following code:

echo $this->Form->input('Story.story_type_id', array('tabindex' => 2));

with this code in the controller to populate the list

$this->set('story_types', $this->Story->StoryType->find('list', array ('order' => 'title')));

But it's not populating the select box with anything. I know that th开发者_Go百科e find() option is working because doing a debug within the controller produces this:

Array
(
    [1] => First Person
    [3] => Third Person
)

The weird thing is that it's exactly the same code, just querying other models, to populate select lists for things like users and genres, it's just the story types that isn't working.

Any ideas? Cheers.


You don't mention which version of CakePHP you're using, but try setting storyTypes rather than story_types:

$this->set( 'storyTypes', $this->Story->StoryType->find( 'list', array( 'order' => 'title' ) ) );

Older versions of CakePHP (pre-1.3) modified set variable names to headlessCamelCase and, even if you're using 1.3.x, there may be a little bit of that infrastructure lingering. It's a bit of a reach, but it's easy enough to test and it seems plausible that this could be the root of your problem.

I'll be curious to see what you find out.


This is a little hacky, but I think it will work:

echo $this->Form->input('Story.story_type_id', array('tabindex' => 2, 'options' => $story_types));


here's what you should really do .. (esp, for version 2.x) - in case if some people are facing the same problem.

[inside your constroller action]

$oneOfTheColumns = 'title'; //just for sake of making it clear - if you have to order the results

$storyTypes = $this->Story->StoryType('find', array('order'=>$oneOfTheColumns));
$this->set(compact('storyTypes'));

[inside your view]

echo $this->Form->input('StoryType');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜