Why model methods create(), save(), update() return always false? I save model and create() return false, but in db all saved.
|
Sep '14 |
5 |
2911 |
-2 |
Why model methods create(), save(), update() return always false? I save model and create() return false, but in db all saved.
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);
}
}
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.
The only way it returns false when:
_preSave
returns false because of an automatic or user-defined validation returns false_postSave
related records cannot be saveA 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;
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