2013-02-03 2 views
1

CakePHP를 배우기 위해 재미있는 사이트를 만들었지 만, 어떤 이유로 인해 선택한 항목을 표시하기 위해 여러 개의 드롭 다운 상자를 선택할 수 없습니다. 이 예에서 비디오 게임은 여러 가지 어려움을 겪을 수 있습니다 (쉽고, 보통, 어렵습니다). 내 게임 편집 페이지에 난 어려움을 선택하는 다중 선택 상자가 있습니다. 세 가지 어려움을 모두 보여 주며 선택할 수 있으며 올바르게 저장됩니다. 그러나 편집 페이지로 돌아 가면 이전에 저장 한 항목이 선택된 것으로 강조 표시되지 않습니다. 레코드가 데이터베이스에 올바르게 저장되고 있는지 확인했습니다.HABTM 드롭 다운 항목이 선택되지 않았습니다.

테이블 : 게임 어려움 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) :

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

이것은 내가 누락 된 단순한 것이어야하지만 HABTM에 대한 책을 읽었으며 여기에서 검색하여 다중 선택 상자를 많이 찾을 수 없었습니다.

UPDATE :

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

컨트롤러에서 수행중인 작업에 따라 다릅니다. 'edit()'함수는 어떻게 생겼는가? 변수'$ game'로 무엇을하고 있습니까? –

답변

8

무엇 CakePHP의 버전을 사용중인 : 또한 여기 더보기에 일부입니다

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


} 

:

여기

컨트롤러의 전체 편집 기능인가? 2.2.6 및 2.3.0 릴리스에는 기존 habtm이 선택되었음을 표시하는 것과 관련된 버그가 있습니다. 따라서 2.2.6을 사용하는 경우 2.2.7로 업데이트되거나 2.3.0을 사용하는 경우 다음 버그 수정 릴리즈가 완료 될 때까지 github에서 master 분기를 사용하십시오.

+0

그게 다야! 나는 2.2.5에 있었고, 2.2.7로 업그레이드하면 코드를 변경하지 않고도 선택한 항목이 표시되기 시작했다. – chill182

+0

2.3.0에서 master 분기로 업그레이드하면 문제가 해결됩니다. – Tarnschaf

+1

2.3.1이 릴리스되어 2.3.0을 사용하는 사람들이이를 업데이트 할 수 있습니다. – ADmad

1

성능상의 이유로 인해 AppModel에 public $recursive = -1;을 설정하거나 컨트롤러에서 재귀를 변경하는 모든 사용자에게 적용됩니다. 자동 재귀는 자동 재귀 1없이 작동하지 않습니다! 편집을 위해 데이터를 회수 할 때 옵션에서 변경해야합니다.

$options = array(
    'recursive' => 1, 
    'conditions' => array('YourModel.' . $this->YourModel->primaryKey => $id) 
); 
$this->request->data = $this->YourModel->find('first', $options); 
관련 문제