Cakephp不保存下拉列表



我有一个关系 游戏属于类型。在游戏的添加视图中,我看到一个带有各种类型标题的下拉列表,这些标题在我的表格中是 VARCHAR。但是当我选择一个并按保存时,它会保存类型的 ID 而不是标题(尽管在我的下拉列表中我选择了标题。当我在PHPmyAdmin中创建游戏时,一切正常。我的代码:

游戏模型

class Game extends AppModel {
    public $name = 'Game';
    public $primaryKey = 'id';
    public $belongsTo =  array
     (
  'Type' => array (
        'className' => 'Type',
        'foreignKey' => 'type_id'
     ));

在游戏控制器中添加功能

 public function add() {
$types = $this->Game->Type->find('list',array('fields' => array('title')));
$this->set('types',$types);
    if ($this->request->is('game')) {
        if ($this->Game->save($this->request->data)) {
            $this->Session->setFlash('Your game has been created.');
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash('Unable to add your game.');
        }
    }
} 

添加类型的视图

    <h1>Add Game</h1>
<?php
echo $this->Form->create('Game',array('action' => 'edit'));
echo $this->Form->input('tablename',array('rows' => '1'));
echo $this->Form->input('date');
echo $this->Form->input('type_id');
echo $this->Form->end('Save Games');
?>

我希望有人能找到解决方案。如果需要更多信息,请随时询问。

提前感谢您和最良好的祝愿。

//Add function in your controller
$types= $this->Game->find('list', array(
    'conditions' => array('Game.type_id'),
    'fields'     => array('Game.id', 'Game.title')
));
$this->set(compact('types'));
// view
echo $this->Form->input('type_id', array(
    'type'    => 'select',
    'options' => $types,
    'empty'   => false
));

最新更新