未选择HABTM下拉项 [英] HABTM dropdown items not selected

查看:96
本文介绍了未选择HABTM下拉项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

表:
游戏
困难
difficulty_games

Tables: games difficulties difficulties_games

模型:

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'
            )
);
}

控制器:

$game = $this->Game->findById($id);
$this->set('difficulties', $this->Game->Difficulty->find('list'));

检视(edit.ctp):

View (edit.ctp):

echo $this->Form->input('Difficulty');

这是一个简单的东西,我缺少,但我已阅读通过HABTM的书,这里,不能找到很多多选框。

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.

UPDATE:

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

Here is the entire edit function in the controller:

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'));


}

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');


推荐答案

版本2.2.6和2.3.0有一个与显示现有habtm选择相关的错误。所以更新到2.2.7如果使用2.2.6或如果使用2.3.0使用从github的主分支,直到下一个bugfix发布完成。

What CakePHP version are you using? Releases 2.2.6 and 2.3.0 have a bug related to showing existing habtm selected. So updated to 2.2.7 if using 2.2.6 or if using 2.3.0 use the master branch from github until the next bugfix release is done.

这篇关于未选择HABTM下拉项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆