在moviesController中,我有一个公共函数add(),它通常需要使用Movies表中的数据。 (例如,我可以执行$ this-> Movie-> findById($ id);或$ this-> Movie-> save();)
在添加视图中,我有一个表单,它从用户获取输入并将其保存到数据库中。 一切正常。
但我想稍微改变一下这个形式,所以会有一个类型列表(动作,喜剧等),用户可以选择其中的很多。 现在我只有一个带有流派值的数组,用于填充复选框,但我需要从表中获取这些值。
所以我要问的问题是,在公共函数add()中从moviesController中的其他表Genres获取行的方法是什么,这样我可以在使用主表的同时填充表单列表?
答案 0 :(得分:0)
如果您的Movies
模型与Genre
有任何关系,您可以这样做:
$this->set('genre', $this->Movie->Genre->find('list'));
在您看来,您可以执行以下操作(伪代码):
<?php
echo $this->Form->input('Movie.Genre', array(
'options' => $genre,
'type' => 'select',
'multiple' => 'checkbox',
'class' => '',
'label' => array('class' => 'control-label', 'text' => "Genre:"),
'after' => ''
));
?>
更新:
当然Movie
应该与Genre
建立一个has_many关系。
答案 1 :(得分:0)
在您的电影控制器中:
<?php
class MoviesController extends AppController {
// here you select wath models you want to use
public $uses = array("Movie", "Genre");
public function add() {
// Here you get genre list ids and names
$genreList = $this->Genre->find("list", array());
}
}
我希望这会有所帮助。