CakePHP数据不保存/添加到数据库

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 ;

You've specified an incorrect request 'method';

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

Should be:

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

I suspect you got confused by the examples on CakePHP, which use a 'post' Model. However this line is to check the type of request used to access the page, e.g. post, get, put.

By checking for the right request method, CakePHP will only insert/update the data is the form is sent/submitted, otherwise the form is just shown without updating the database

You forget to define name property inside model.

var $name = 'Sm';

Just a few minutes ago, I was facing same type of problem. My problem was more weird. In my controller I used something like this:

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

As you can see I've handled the negative case but still I could see nothing. Even in my model, I used beforeSave and afterSave to check. In my beforeSave model, I could see a perfectly formatted array ready to be saved but I afterSave was not triggering which means the data was not saved. Hence I should see error from my controller. Still no solution.

Now this is how I figured out the problem. I checked the table the data is gonna be saved into. The table had lots of columns with NOT NULL attribute. The data I was saving had some fields with NULL values. So theoretically, I should see validationErrors in the controller as a reason but unfortunately it was not showing. Setting those fields nullable solved my problem. So my suggestion to you is to check which fields might have NULL value and set those nullable and make sure the NOT NULL fields have some values.

Hope this helps. Cheers!!!