3

Submitting the data only reloads the page, no errors or messages is given by CakePHP. The code follows the same/similar structure as the blog tutorial.

The view code

        <?php
        echo $this->Form->create('Sm');
        echo $this->Form->input('recievers', array('rows' => '1'));
        echo $this->Form->input('subject');
        echo $this->Form->input('message');
        echo $this->Form->end('SEND');
        ?>

Controller code

    public function send() {
    if ($this->request->is('sm')) {
        $this->Sm->create();
        if ($this->Sm->save($this->request->data)) {
            $this->Session->setFlash('Sms has been added to the database');
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash('Unable to send sms.');
        }
    }
}

Model code

class Sm extends AppModel {
public $validate = array(
    'subject' => array(
        'rule' => 'notEmpty'
    ),
    'message' => array(
        'rule' => 'notEmpty'
    ),
    'recievers' => array(
        'rule' => 'notEmpty'
    )
); }

Exported SQL

    CREATE TABLE IF NOT EXISTS `sms` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `subject` varchar(150) DEFAULT NULL,
  `message` text,
  `sender` varchar(50) DEFAULT NULL,
  `recievers` text,
  `sent` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;
4

3 回答 3

4

您指定了错误的请求“方法”;

if ($this->request->is('sm')) {

应该:

if ($this->request->is('post')) {

我怀疑您对 CakePHP 上使用“post”模型的示例感到困惑。然而,这一行是检查用于访问页面的请求类型,例如post, 。getput

通过检查正确的请求方法,CakePHP 将只插入/更新表单发送/提交的数据,否则仅显示表单而不更新数据库

于 2013-03-04T14:07:47.723 回答
0

就在几分钟前,我遇到了同样类型的问题。我的问题更奇怪。在我的控制器中,我使用了这样的东西:

if( $this->MyModel->save( $this->request->data ) ) {
    //positive. proceed.....
}
else {
    //negative. show error
    pr( $this->MyModel->validationErrors );
}

如您所见,我已经处理了负面情况,但我仍然什么也看不到。即使在我的模型中,我也使用 beforeSave 和 afterSave 进行检查。在我的 beforeSave 模型中,我可以看到一个格式完美的数组可以保存,但我 afterSave 没有触发,这意味着数据没有保存。因此我应该从我的控制器中看到错误。仍然没有解决办法。

现在这就是我发现问题的方式。我检查了要保存数据的表。该表有很多具有 NOT NULL 属性的列。我保存的数据有一些具有 NULL 值的字段。所以理论上,我应该将控制器中的validationErrors视为一个原因,但不幸的是它没有显示。将这些字段设置为空解决了我的问题。所以我给你的建议是检查哪些字段可能具有 NULL 值并设置那些可为空的并确保 NOT NULL 字段具有一些值。

希望这可以帮助。干杯!!!

于 2015-01-27T10:10:40.653 回答
0

您忘记name在模型中定义属性。

var $name = 'Sm';
于 2013-03-04T12:15:45.907 回答