Note that there are some explanatory texts on larger screens.

plurals
  1. POHABTM dropdown items not selected
    primarykey
    data
    text
    <p>I created a site for fun to learn CakePHP but for some reason I can't get a multiple select dropdown box to show my selected items. In this example 1 video game can have multiple difficulties (easy, normal, hard). On my Game edit page I have a multi select box to pick the difficulties. It shows all three difficulties and I can select them and it saves correctly. However when I return to the edit page the items I previously saved do not show highlighted as selected. I have verified that the records are saving correctly in the database.</p> <p>Tables: games difficulties difficulties_games</p> <p>Models:</p> <pre><code>class Game extends AppModel { public $actsAs = array('Containable'); public $hasAndBelongsToMany = array( 'Difficulty' =&gt; array( 'className' =&gt; 'Difficulty', 'joinTable' =&gt; 'difficulties_games', 'foreignKey' =&gt; 'game_id', 'associationForeignKey' =&gt; 'difficulty_id', 'unique' =&gt; 'true' ) ); } class Difficulty extends AppModel { public $actsAs = array('Containable'); public $hasAndBelongsToMany = array( 'Game' =&gt; array( 'className' =&gt; 'Game', 'joinTable' =&gt; 'difficulties_games', 'foreignKey' =&gt; 'difficulty_id', 'associationForeignKey' =&gt; 'game_id', 'unique' =&gt; 'true' ) ); } </code></pre> <p>Controller:</p> <pre><code>$game = $this-&gt;Game-&gt;findById($id); $this-&gt;set('difficulties', $this-&gt;Game-&gt;Difficulty-&gt;find('list')); </code></pre> <p>View (edit.ctp):</p> <pre><code>echo $this-&gt;Form-&gt;input('Difficulty'); </code></pre> <p>This has to be something simple I am missing but I've read through the book on HABTM and searched here and couldn't find much on multi-select boxes. </p> <p>UPDATE:</p> <p>Here is the entire edit function in the controller:</p> <pre><code>public function edit($id = null) { if (!$id) { throw new NotFoundException(__('Invalid post')); } $game = $this-&gt;Game-&gt;findById($id); if (!$game) { throw new NotFoundException(__('Invalid post')); } if ($this-&gt;request-&gt;is('post') || $this-&gt;request-&gt;is('put')) { $this-&gt;Game-&gt;id = $id; if ($this-&gt;Game-&gt;saveAll($this-&gt;request-&gt;data)) { $this-&gt;Session-&gt;setFlash('Your game has been updated.'); $this-&gt;redirect(array('action' =&gt; 'index')); } else { $this-&gt;Session-&gt;setFlash($this-&gt;Game-&gt;invalidFields()); } } if (!$this-&gt;request-&gt;data) { $this-&gt;request-&gt;data = $game; } $this-&gt;set('systems', $this-&gt;Game-&gt;System-&gt;find('list')); $this-&gt;set('genres', $this-&gt;Game-&gt;Genre-&gt;find('list')); $this-&gt;set('difficulties', $this-&gt;Game-&gt;Difficulty-&gt;find('list')); } </code></pre> <p>Also here is some more on the View:</p> <pre><code>echo $this-&gt;Form-&gt;create('Game'); echo $this-&gt;Form-&gt;input('name'); echo $this-&gt;Form-&gt;input('system_id'); echo $this-&gt;Form-&gt;input('genre_id'); echo $this-&gt;Form-&gt;input('Difficulty'); echo $this-&gt;Form-&gt;input('id', array('type' =&gt; 'hidden')); echo $this-&gt;Form-&gt;end('Save Game'); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload