cakephp中的save()函数保存空值

时间:2014-07-11 13:26:44

标签: cakephp frameworks save blogs

我的问题是我不知道save()表格中的null cakephp插入posts值的功能!

当我尝试添加帖子时:http://i.stack.imgur.com/fhRVn.png

重定向插入的数据后,nullhttp://i.stack.imgur.com/YstBb.png

My PostsController.php是:

App::uses('AppController', 'Controller');

class PostsController extends AppController{

    public $components = array('Session');
    public $helpers = array('Html', 'Form');

    public function index() {
        $this -> set('posts', $this->Post->find('all'));
    }

    public function view($id = null){
        if(!$id){
            throw new NotFoundException(_('il faut choisir un post'));
        }
        $post=$this->Post->findById($id);
        if(!$post){
            throw new NotFoundException(_('invalide Post !'));
        }
        $this->set('post',$post);
    } // fin de "view"

    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'));
            }
            $this->Session->setFlash(__('Unable to add your post.'));
        }
        //debug($this->Post->validate);

    }//fin d'ajout
} // fin de "appcontroller"

我的Post.php模型文件是:

class Post extends appModel {

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

我的观点表格是:

echo $this->Form->create('post');
echo $this->Form->input('title');
echo $this->Form->input('body',array('rows' => '3'));
echo $this->Form->end('Ajouter');

1 个答案:

答案 0 :(得分:2)

在您的视图中,您的问题可能是您的模型不发布

echo $this->Form->create('Post');
echo $this->Form->input('title');
echo $this->Form->input('body',array('rows' => '3'));
echo $this->Form->end('Ajouter');