首页 文章

cakephp - 从其他表中获取行

提问于
浏览
0

在moviesController中我有一个公共函数add(),它通常需要处理Movies表中的数据 . (例如我可以做$ this-> Movie-> findById($ id);或$ this-> Movie-> save();)

在添加视图中,我有一个表单,它从用户获取输入并将其保存到数据库中 . 一切正常 .

但我想稍微改变这个形式,所以会有一个类型列表(动作,喜剧等),用户将能够选择其中的许多 . 现在我只有一个带有流派值的数组,用于填充复选框,但我需要从表中获取这些值 .

所以我问的问题是,在publicController的公共函数add()中获取其他表Genres中的行的方式是什么,所以我可以在使用主表时填充表单列表?

2 回答

  • 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());
    
      }
    }
    

    我希望这有帮助 .

  • 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关系 .

相关问题