2014-09-30 30 views
0

我是CakePHP的新手,我創建簡單的表單來添加/編輯/刪除發佈,因爲它是在其官方網站上解釋,我的問題,同時添加發布到數據庫它不是保存數據,它正在創建空白項數據庫cakephp save()創建空白條目到數據庫

這裏是代碼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' 
    ) 
); 
} 
?> 

這裏是查看添加代碼。C tp:

<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'); 
?> 

任何人都可以建議我是什麼導致數據庫的空白條目?

+0

你可以試試這個:調試($這個 - >請求 - >數據)? – deadman 2014-09-30 10:33:35

回答

2

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'); 
?> 
+0

它與我的問題有什麼關係? – utsav 2014-09-30 11:09:11

+0

確定更改後發佈解決我的問題... – utsav 2014-09-30 11:14:49

0

config/core.php文件更改debug label to 2(Configure::write('debug', 2))並嘗試再次保存。型號名稱應在表格中張貼。保存前請檢查發佈數據。

debug($this->request->data);

exit; 
+0

我已經調試值更改爲2和數據陣列,它是什麼樣子: 陣列 ( [交] =>數組 ( [標題] => ASD [體] => ASD測試 ) ) – utsav 2014-09-30 11:07:52