【问题标题】:cakephp form helper: data not savingcakephp 表单助手:数据未保存
【发布时间】:2012-07-18 15:52:27
【问题描述】:

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

【问题讨论】:

  • 你能显示保存的代码吗?
  • 已解决。表单声明中的 s。
  • 感谢您阅读我的问题。
  • Form->create('ApplicationMemberships', array( 'url' => array('controller' => 'ApplicationMemberships', 'action' => 'add '))); ?> 是错误的,因为create('ApplicationMemberships' 中有多余的 s

标签: php cakephp


【解决方案1】:

错误是create('ApplicationMemberships')中多余的s。

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

create('ApplicationMembership')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多