We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Why model methods create(), save(), update() return always false?

Why model methods create(), save(), update() return always false? I save model and create() return false, but in db all saved.



98.9k

It means the record cannot be saved because there are validation messages you have to fix to successfully save the record:

if ($robot->save() == false ) {
    foreach ($robot->getMessage() as $message) {
        var_dump($message);
    }
}


8.0k
edited Aug '14

I have no validation rules in model and record saved sucessfully. I see it in db manager. I dont need any validation. I am trying your code, and getMessages() return empty array.

edited Aug '14

Because you have a wrong SQL syntax or as @Phalcon said above or doesn't have filled some fields which are not defined as default NULL, exactly something went wrong in executing SQL then save will return false.



98.9k

The only way it returns false when:



8.0k
  1. About _preSave. What does the automatic validation mean? All fields in my model filled and saved sucessfully.
  2. About _postSave. I have no related models.

I still can't understand why the model is successfully kept in base, but returns false



8.0k
edited Aug '14

I use phalcon 1.3.2 Win8 Can anybody say me why save() always return false. This is bug. This is very bad bug! In my code dont work construction as:

if ($this->save()) { return $this->task_id; }

Maybe you have NOT NULL column(s) which is not set?



8.0k

No. All fields are filled and model saved successfully.



8.0k
edited Sep '14

A create CodeCeption test. And it fails.

<?php

class Test extends \Phalcon\Mvc\Model
{

    public function getSource()
    {
        return 'sk_test';
    }

}

class TestTest extends Codeception\TestCase\Test
{
    function testSave()
    {

        $test = new \Test;
        $test->val = 1;

        // performing assertion
        $this->assertTrue($test->save());
        $this->assertEquals($test->val, 1);
    }
}

Result


There was 1 failure:

---------
1) TestTest::testSave
Failed asserting that false is true.

Table DDL

CREATE TABLE `sk_test` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `val` int(10) unsigned DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;    
edited Sep '14

Can you add this:

    $save = $test->save();

    // performing assertion
    $this->assertEquals(count($test->getMessages()), 0);
    $this->assertTrue($save);
    $this->assertEquals($test->val, 1);


8.0k
edited Sep '14

No messages, count equals 0.

There was 1 failure:

---------
1) TestTest::testSave
Failed asserting that false is true.     


8.0k
edited Sep '14

Events log

[Sun, 14 Sep 14 14:38:02 +0800][INFO] beforeValidation  
[Sun, 14 Sep 14 14:38:02 +0800][INFO] beforeValidationOnCreate  
[Sun, 14 Sep 14 14:38:02 +0800][INFO] validation  
[Sun, 14 Sep 14 14:38:02 +0800][INFO] afterValidationOnCreate  
[Sun, 14 Sep 14 14:38:02 +0800][INFO] afterValidation  
[Sun, 14 Sep 14 14:38:02 +0800][INFO] beforeSave  
[Sun, 14 Sep 14 14:38:02 +0800][INFO] beforeCreate  
[Sun, 14 Sep 14 14:38:02 +0800][INFO] notSave  
[Sun, 14 Sep 14 14:38:02 +0800][INFO] notSaved  


8.0k

All thanks! I apologize, there was a mistake in my project. Incorrectly i redefined method insert in mysql adapter. It return nothing.