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

Phalcon Model beforeSave method not working

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.

Can you show some code? All events works for me in 1.3.4 and 2.0.* versions

Its working for me, show us code.

edited Nov '15

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

Is it solved your issue?

I am understand, it work very well thanks alot !

Is it solved your issue?