我有以下表格
articles (Article model)
- id
- topic_id
articles_topics (ArticleTopic model)
- id当我在ArticlesController add()中时,我成功地生成了不同主题的选择列表,如下所示:
$this->set('topics', $this->Article->ArticleTopic->find('list'));我的文章add.ctp视图使用以下代码显示一个填充的选择列表:
echo $this->Form->input('topic_id', array('empty' => 'Select a Topic'));这里一切都很好。我的问题是其他模式,如Sidebar和SidebarLocation。它们之间有着恰当的联系。
这些表是
sidebars (Sidebar model)
- id
- sidebar_location_id
sidebars_locations (SidebarLocation model)
- id我尝试在我的SidebarsController add()中设置以下内容
$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但是,侧栏add.ctp上的选择列表没有填充。它不是一个空数组。如果我把它打印出来,它就能用。
同样,我对表单使用了以下代码。
echo $this->Form->input('sidebar_location_id', array('empty' => 'Select a Location'));我知道这个问题是因为这个专栏的标题是"sidebar_location_id".如果我将此重命名为"location_id“并将模型SidebarLocation.php重命名为Location.php,则可以工作,但我不能这样做,因为有多个其他模型具有关联的位置类型模型(即:公司模型与使用外键company_location_id的CompanyLocation模型相关联)。我需要这些单独的SidebarLocation和CompanyLocation模型来区分。
编辑:,我想回答我自己的问题,我可以用我的形式
echo $this->Form->input('sidebar_location_id',
array('type'=>'select','options'=> $sidebar_locations)
);但我希望如果有人知道我能继续用我和其他模特一样的方法
echo $this->Form->input('sidebar_location_id');发布于 2013-05-11 20:02:10
鲍勒雷,
您需要以以下方式从控制器中设置您的值:
$sidebarLocations = $this->Sidebar->SidebarLocation->find('list');
$this->set(compact('sidebarLocations'));在你的CTP里
echo $this->Form->input('sidebar_location_id');https://stackoverflow.com/questions/16500869
复制相似问题