0
votes

In moviesController i have a public function add() which normally needs to work with data from Movies table. (For example i can do $this->Movie->findById($id); or $this->Movie->save(); )

In the add view I have a form which takes input from a user and save it into database. All that works just fine.

But i would like to change this form a little, so there will be a list of genres (action, comedy, etc) and user will be able to select many of them. For now i just have an array with genre values which are used to populate checkboxes but I need to get this values from a table.

So the question I'm asking is, what is the way to get rows from other table Genres in moviesController in public function add() so i can populate a form list while still using main table?

2

2 Answers

0
votes

In your Movies Controller:

<?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());

  }
}

I hope this helps.

0
votes

If your Movies model has any relationship with Genre, you can just do this:

$this->set('genre', $this->Movie->Genre->find('list'));

In your view, you can do something like this (pseudocode):

<?php
    echo $this->Form->input('Movie.Genre', array(
        'options' => $genre,
        'type' => 'select',
        'multiple' => 'checkbox',
        'class' => '',
        'label' => array('class' => 'control-label', 'text' => "Genre:"),
        'after' => ''
    ));
?>

Update:

Of course Movie should have a has_many relationship with Genre.