Why beforeSave, beforeUpdate, and some event in Model class not working when I try to Save Record ? Both 1.x and 2.x version are the same problem. Thanks for reading.
|
Nov '15 |
4 |
2716 |
0 |
My Code:
<?php
use Phalcon\Mvc\Model;
class Robots extends Model
{
public $id;
public $name;
public $created_at;
public function beforeSave()
{
// Convert the array into a string
$this->created_at = time();
var_dump($this);
die("Here");
}
}
// In controller
$robot = new Robots();
$robot->name = "Utron";
$robot->save();
// Why the execution not stop in beforeSave ?? And my $robot not saved. I see the created_at is null
In this case, your code not works, because model stops in validation and throws exception. Try to use timestampable behavior to fill created_at value or beforeValidation method to do that manually.
My Code:
<?php
use Phalcon\Mvc\Model;
class Robots extends Model { public $id;
public $name; public $created_at; public function beforeSave() { // Convert the array into a string $this->created_at = time(); var_dump($this); die("Here"); }
} // In controller $robot = new Robots(); $robot->name = "Utron"; $robot->save();
// Why the execution not stop in beforeSave ?? And my $robot not saved. I see the created_at is null
In this case, your code not works, because model stops in validation and throws exception. Try to use timestampable behavior to fill created_at value or beforeValidation method to do that manually.
My Code:
<?php
use Phalcon\Mvc\Model;
class Robots extends Model { public $id;
public $name; public $created_at; public function beforeSave() { // Convert the array into a string $this->created_at = time(); var_dump($this); die("Here"); }
} // In controller $robot = new Robots(); $robot->name = "Utron"; $robot->save();
// Why the execution not stop in beforeSave ?? And my $robot not saved. I see the created_at is null
Thanks a lot bro :D