【问题标题】:cakephp save() creating blank entry to databasecakephp save() 为数据库创建空白条目
【发布时间】:2014-09-30 19:03:13
【问题描述】:

我是 CakePHP 的新手,我正在创建简单的表单来添加/编辑/删除帖子,正如它在其官方网站上解释的那样,我的问题,在将帖子添加到数据库时它不保存数据,它正在创建空白条目数据库

这是 Postscontroller.php 的代码

    <?php
    class PostsController extends AppController {
    public $helpers = array('Html', 'Form');
    public $components = array('Session');




    public function add() {
     if ($this->request->is('post')) {
        $this->Post->create();
        if ($this->Post->save($this->request->data)) {
            $this->Session->setFlash(__('Your post has been saved.'));

            return $this->redirect(array('action' => 'index'));
        }
        //debug($this->Post->validationErrors);
        $this->Session->setFlash(__('Unable to add your post.'));
    }
   }




 }
    ?>

这是模型 Post.php 的代码:

<?php

class Post extends AppModel {

 public $validate = array(
    'title' => array(
        'rule' => 'notEmpty'
    ),
    'body' => array(
        'rule' => 'notEmpty'
    )
);
}
?>

这是查看 add.ctp 的代码:

<h1>Add Post</h1>
<?php
echo $this->Form->create('post');
echo $this->Form->input('title');
echo $this->Form->input('body');
echo $this->Form->end('Save Post');
?>

谁能建议我是什么导致数据库出现空白条目?

【问题讨论】:

  • 你可以试试这个:debug($this->request->data) ??

标签: php cakephp cakephp-model


【解决方案1】:

add.ctp

表单名称是 Post not post...

<h1>Add Post</h1>

<?php
echo $this->Form->create('Post');
echo $this->Form->input('title');
echo $this->Form->input('body');
echo $this->Form->end('Save Post');
?>

【讨论】:

  • 这跟我的问题有什么关系?
【解决方案2】:

config/core.php 文件中更改debug label to 2.(Configure::write('debug', 2)) 并再次尝试保存。模型名称应为表单中的 Post。保存前请检查帖子数据。

debug($this-&gt;request-&gt;data);

exit;

【讨论】:

  • 我已经将调试值更改为 2 和数据数组,它看起来像这样: Array ( [post] => Array ( [title] => asd [body] => asd test ) )
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-18
相关资源
最近更新 更多