【问题标题】:Basic: CakePHP data not being saved nor validation working?基本:CakePHP 数据未保存或验证工作?
【发布时间】:2012-05-26 02:06:17
【问题描述】:

我想知道的是一个两部分的问题。

为什么验证没有做任何事情,为什么数据不会发布到数据库?

问题控制器.php

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

public function view($id = null) {
    $this->Question->id = $id;
    $this->set('question', $this->Question->read());

}

public function ask() {

    if ($this->request->is('post')) {
        if ($this->Question->save($this->request->data)) {
            $this->Session->setFlash('Your Question has been asked.');
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash('Unable to ask your question.');
        }
    }
}
}
  ?>

问题模型

 class Question extends AppModel {

   public $validate = array(
    'question' => array(
        'rule' => 'notEmpty'
    ),
);
}

表 postgreSQL

CREATE TABLE questions (
id serial not null unique primary key,
question varchar(245),
timecreated timestamp default CURRENT_TIMESTAMP

)

问.ctp

<?php
 echo $this->Form->create('Post');
 echo $this->Form->input('question');
 echo $this->Form->end('Ask');
?>

【问题讨论】:

    标签: php postgresql cakephp pdo


    【解决方案1】:

    问题可能是您在创建表单时指定了错误的模型。下面一行

    echo $this->Form->create('Post');
    

    应该是

    echo $this->Form->create('Question');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-08
      • 2010-12-18
      • 1970-01-01
      • 1970-01-01
      • 2021-02-10
      • 2012-01-16
      • 1970-01-01
      相关资源
      最近更新 更多