私は CakePHP フォーム ヘルパー ( cakephp doc ) を使っていくつかの成功を収めてきました。今、テーブルのモデルに関係のないビューにあるフォームを作成しようとしています。私はドキュメントに従い、それを私の見解に入れました:
<?php echo $this->Form->create('ApplicationMemberships', array(
'url' => array('controller' => 'ApplicationMemberships', 'action' => 'add'))); ?>
<fieldset >
<?php
echo $this->Form->input('chantier_id');
echo $this->Form->input('application_id');
echo $this->Form->input('ponderation',array('type' => 'number'));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
次のコードをコントローラに挿入して、フィールドに入力されたデータを取得します。
$chantiers = $this->Chantier->find('list');
$this->loadModel('Application');
$applications = $this->Application->find('list');
$this->set(compact('chantiers', 'applications'));
すべてうまくいきました。フォームができて、データを入力すると、データが保存されたことを示すフラッシュが表示されました。問題: データがデータベースに追加されていません。
編集:
保存を行うコードは次のとおりです。
public function add() {
if ($this->request->is('post')) {
$this->ApplicationMembership->create();
if ($this->ApplicationMembership->save($this->request->data)) {
$this->Session->setFlash(__('The application membership has been saved'));
//$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The application membership could not be saved. Please, try again.'));
}
}
$chantiers = $this->ApplicationMembership->Chantier->find('list');
$applications = $this->ApplicationMembership->Application->find('list');
$this->set(compact('chantiers', 'applications'));
}