2012-07-19 117 views
0

我一直在玩CakePHP表單助手(cakephp doc),並取得了一些成功。現在我正在創建一個與表格模型無關的表單。我跟着文檔,並把在我看來:cakephp表單助手:數據不保存

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

您能否顯示執行保存的代碼? – Stian 2012-07-19 12:54:08

+0

已添加代碼。 qsdgfsdqfgsdgf – 2012-07-19 12:58:38

+0

已解決。表格聲明中的s。 – 2012-07-19 13:02:02

回答

0

錯誤是多餘的S IN create('ApplicationMemberships')

根據Cakephp的約定,控制器應該是複數和模型單數。因此,爲了創建相關模型的實例,我應該寫下:

create('ApplicationMembership')