未选择HABTM下拉项目

Roc*_*e C 1 cakephp

我创建了一个有趣的网站来学习CakePHP但由于某种原因我无法获得多选下拉框以显示我选择的项目.在这个例子中,1视频游戏可能有多个困难(简单,正常,困难).在我的游戏编辑页面上,我有一个多选框来挑选困难.它显示了所有三个困难,我可以选择它们并正确保存.但是,当我返回编辑页面时,我之前保存的项目不会突出显示为已选中.我已经验证记录在数据库中正确保存.

表:游戏困难困难_游戏

楷模:

class Game extends AppModel {
public $actsAs = array('Containable');
public $hasAndBelongsToMany = array(
    'Difficulty' =>
        array(
            'className' => 'Difficulty',
            'joinTable' => 'difficulties_games',
            'foreignKey' => 'game_id',
            'associationForeignKey' => 'difficulty_id',
            'unique' => 'true'
            )
);
}
class Difficulty extends AppModel {
public $actsAs = array('Containable');
public $hasAndBelongsToMany = array(
    'Game' =>
        array(
            'className' => 'Game',
            'joinTable' => 'difficulties_games',
            'foreignKey' => 'difficulty_id',
            'associationForeignKey' => 'game_id',
            'unique' => 'true'
            )
);
}
Run Code Online (Sandbox Code Playgroud)

控制器:

$game = $this->Game->findById($id);
$this->set('difficulties', $this->Game->Difficulty->find('list'));
Run Code Online (Sandbox Code Playgroud)

查看(edit.ctp):

echo $this->Form->input('Difficulty');
Run Code Online (Sandbox Code Playgroud)

这必须是我想要的简单但我已经阅读了关于HABTM的书并且在这里搜索并且在多选框上找不到多少.

更新:

这是控制器中的整个编辑功能:

public function edit($id = null) {
    if (!$id) {
        throw new NotFoundException(__('Invalid post'));
    }

    $game = $this->Game->findById($id);
    if (!$game) {
        throw new NotFoundException(__('Invalid post'));
    }
    if ($this->request->is('post') || $this->request->is('put')) {
        $this->Game->id = $id;
        if ($this->Game->saveAll($this->request->data)) {
            $this->Session->setFlash('Your game has been updated.');
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash($this->Game->invalidFields());
        }
    }

    if (!$this->request->data) {
        $this->request->data = $game;
    }
    $this->set('systems', $this->Game->System->find('list'));
    $this->set('genres', $this->Game->Genre->find('list'));
    $this->set('difficulties', $this->Game->Difficulty->find('list'));


}
Run Code Online (Sandbox Code Playgroud)

另外还有一些关于视图的内容:

echo $this->Form->create('Game');
echo $this->Form->input('name');
echo $this->Form->input('system_id');
echo $this->Form->input('genre_id');
echo $this->Form->input('Difficulty');
echo $this->Form->input('id', array('type' => 'hidden'));
echo $this->Form->end('Save Game');
Run Code Online (Sandbox Code Playgroud)

ADm*_*mad 8

您使用的CakePHP版本是什么?版本2.2.6和2.3.0有一个与显示已选择的现有habtm相关的错误.如果使用2.2.6或者如果使用2.3.0则使用github的master分支直到下一个bugfix发布完成后更新到2.2.7.