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 ;
2
  • Up voted the question as I saw no direct reason that this question earned a negative score. All the required code is included, neatly formatted and the OP is trying to start CakePHP based on the example in the manual. Commented Mar 4, 2013 at 14:18
  • Thank you kind sir :) As for the question, my answer in the end was to use the bake tool to generate the code instead. Neither of the suggested answers below did the trick. Might spend some time comparing my original code with the generated code to find the correct answer. Commented Mar 5, 2013 at 16:15

3 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

3 Comments

Ah yes! I got confused with the post model and the post method, was pulling my hair all day over this simple error. Thank you!
Perhaps post was not a good name to be used in the examples :)
@arthur nope not really. I've seen other people as well being confused by this :)
0

You forget to define name property inside model.

var $name = 'Sm';

2 Comments

You don't have to specify the 'name' property, it is automatically set to the className; api.cakephp.org/2.3/source-class-Model.html#691
Since 2.x it is also preferred by most (and recommended by many) not to set it at all.
0

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!!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.