2012-04-30 44 views
0

我有一个关系游戏belongsTo类型。在游戏的添加视图中,我看到一个下拉列表,其中包含各种类型的标题,这些标题在我的表中都是VARCHAR。但是,当我选择一个,然后按保存,这样可以节省型的,而不是标题的ID(eventhough在我的下拉列表中,我选择了标题当我创建在phpMyAdmin一场比赛,一切工作正常 我的代码:。Cakephp不保存下拉列表

在GameController

GameModel

class Game extends AppModel { 

    public $name = 'Game'; 
    public $primaryKey = 'id'; 
    public $belongsTo = array 

    (
    'Type' => array (
     'className' => 'Type', 
     'foreignKey' => 'type_id' 
    )); 

add函数

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

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

谢谢你在前进和良好的祝愿。

回答

0
//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 
)); 
+0

sry刚刚发现我使用“types_id”而不是type_id,但现在我有下一个问题,但非常感谢您的极快响应!你能帮我解决我的新问题吗? –

+0

当然,新问题是什么? –

+0

我编辑了最初的帖子!只要阅读前几行。实际上,如果标题(VARCHAR),它会保存我的类型的ID。尽管在我的Dropdown中,我可以看到选择的标题。表是正确的格式,如果我在PHPmyadmin中创建一个游戏,一切工作正常。 –