AutoMagic select box not populated in CakePHP

I have the following relationship setting between two models

  • The story belongs to StoryType.
  • StoryType hasMany Story

I set up a form to select a 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 he does not fill the selection field with anything. I know that the find () function works because doing debugging inside the controller produces this:

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

The strange thing is that this is exactly the same code, just requesting other models to fill in the pick lists for things like users and genres, these are just story types that don't work.

Any ideas? Greetings.

+1
source share
3 answers

You do not specify which version of CakePHP you are using, but try setting storyTypes , not story_types :

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

Older versions of CakePHP (pre-1.3) modified the names of the given variables for headlessCamelCase and, even if you use 1.3.x, there may be a little lag in this infrastructure. This is a little reachable, but easy enough to check, and it seems plausible that this could be causing your problem.

I will be interested to know what you have learned.

+5
source

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

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

here is what you really have to do .. (esp, for version 2.x) - in case some people are faced with the same problem.

[inside your action in the constructor]

 $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'); 
+1
source

Source: https://habr.com/ru/post/1340539/


All Articles