0

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

1 回答 1

0

错误是create('ApplicationMemberships').

根据 Cakephp 的约定,控制器应该是复数形式,而模型应该是单数形式。因此,为了创建关联模型的实例,我应该编写:

create('ApplicationMembership')
于 2012-08-03T00:26:35.587 回答