cakephp表单助手:数据不保存

时间:2012-07-19 12:50:34

标签: php cakephp

我一直在玩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'));
}

1 个答案:

答案 0 :(得分:0)

错误是create('ApplicationMemberships')中的额外内容。

根据Cakephp的惯例,控制器应该是复数和模型单数。所以为了创建关联模型的实例,我应该写:

create('ApplicationMembership')