onBeforeSave() doesn't seem to work?

I am certain this is just a silly mistake somewhere in my code but onBeforeSave() doesn’t seem to be working as I expect.

I basically have this:




public function onBeforeSave(){

       if($this->isNewRecord){

          $this->ordertime = time();

          $this->status = 1;

          }

        }



I however get an error saying that the status field is empty. I am just trying to set them when a record is inserted for the first time. Am I missing something?

maybe the beforeSave method is not beeing called properly so it doesnt call the onBeforeSave event

I think you wanted this:




public function beforeSave()

{

    if (parent::beforeSave())

    {      

        if ($this->isNewRecord)

        {

          $this->ordertime = time();

          $this->status = 1;

        }

        return true;

    }



Do you get the error because you have a ‘required’ validation rule? If so you may want to use beforeValidate() instead (or you can inhibit exclude validation by using a scenario).

/Tommy

Thanks Tommy, that was it! I knew I was doing something silly :P