2012-07-19 112 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')