문제

I have the following tables

articles (Article model)
- id
- topic_id

articles_topics (ArticleTopic model)
- id

When I am in the ArticlesController add() I successfully generate a select list of the different topics to choose from like so:

$this->set('topics', $this->Article->ArticleTopic->find('list'));

My articles add.ctp view displays a populated select list using the following code:

echo $this->Form->input('topic_id', array('empty' => 'Select a Topic'));

All good here. my issue is with other models such as Sidebar and SidebarLocation. These are linked properly.

The tables are

sidebars (Sidebar model)
- id
- sidebar_location_id

sidebars_locations (SidebarLocation model)
- id

I have tried setting in my SidebarsController add() the following

$this->set('sidebar_locations', $this->Sidebar->SidebarLocation->find('list'));
$this->set('sidebarlocations', $this->Sidebar->SidebarLocation->find('list'));
$this->set('locations', $this->Sidebar->SidebarLocation->find('list'));

// I have also used compact for this

However, the select list on sidebars add.ctp is not populating. IT IS NOT AN EMPTY ARRAY. If I print it, it works.

Again I am using the following code for the form.

echo $this->Form->input('sidebar_location_id', array('empty' => 'Select a Location'));

I know that the issue is because the column is titled "sidebar_location_id". It works if I rename this to "location_id" and rename the model SidebarLocation.php to Location.php but I cannot do this because there are multiple other models that have associated location-type models (ie: Company model is associated to CompanyLocation model using the foreign key company_location_id). I need these separate SidebarLocation and CompanyLocation models for the distinction.

EDIT: I guess to answer my own questions I can use in my form

echo $this->Form->input('sidebar_location_id', 
    array('type'=>'select','options'=> $sidebar_locations)
);

But I'm hoping if anyone knows a way I can continue to use JUST as I can with other models

echo $this->Form->input('sidebar_location_id');
도움이 되었습니까?

해결책

Bowlerae,

You need to set your value from controller in below way:

$sidebarLocations = $this->Sidebar->SidebarLocation->find('list');
$this->set(compact('sidebarLocations'));

And in your CTP

echo $this->Form->input('sidebar_location_id');
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top