3

I am aware of functionality of function beforeSave() in YII that this function is used to perform something, which we want to perform before our data saved.

However, as far as we want to implement this before our data got save to database, can't we directly write this code before save() is calling (-> save () is storing out records to database )

Hence, I am not sure why exactly we need to create specific function like beforeSave () to perform action which we need to fire before Save() called, when we directly write that code before save() line as well.

can someone please explain this ? I have searched lot for reason of this but on every page, it redirect to explanation of beforeSave() function only.

Bijan
  • 6,011
  • 13
  • 64
  • 120
CodeWithCoffee
  • 1,850
  • 1
  • 12
  • 33

2 Answers2

12

Yii and other MVC frameworks have those kind of functions.

While you can write your "before save" code in the controller, prior to the save() function - it's more recommended and useful to use the beforeSave() function.

Reason 1: The M in the MVC

The beforeSave relates to the model, so it would be more logical to have a code that handles the model's properties (fields) in the model's file rather than having that code in the controller.

Reason 2: Saving is for insert & update

You use save() when you insert a new record and also when you update an existing record. Without using the beforeSave built-in function, you'll have to have 2 instances of your "manual" before save code. ("Waste" of code lines)

Reason 3: Saving a model from another controller

What if you'll be asked to expand your application and now you'd have to face a new controller that need to save that same model (from some reason - just a possible scenario) - you'll have to copy your "before save" code to that controller. While if you're using the built-in beforeSave function - you don't.

In conclusion, the main purpose of frameworks is to reduce the code you need to write while keeping anything logical (MVC separation). While you can do things differently, why not using what's already exists?

Ofir Baruch
  • 9,897
  • 1
  • 23
  • 37
4

A simple example:

I have a table with two date fields. Every time I try to perform insert or update I need to get current system date and make my operation depending on operation type.

public function beforeSave() {

    if ($this->isNewRecord) {
        $this->insertDate = new CDbExpression('NOW()');
    } else {
        $this->updateDate = new CDbExpression('NOW()');
    }

    return parent::beforeSave();
}

I wrote this once so I dont have to write everytime I call save() on that object.

Also some databases prefer different time formats so you can handle them here:

public function beforeSave() {
    $this->date = date('Y-m-d', $this->date);
    return parent::beforeSave();
}
Ghokun
  • 2,679
  • 3
  • 22
  • 29
  • Thanks !! So basically wherever user wrote beforeSave() , it will default call before actuall save() function get called ,correct ? As you given example, we can insert / update date and that is fine, what if we want to add / edit some record which we have added through form ? is that possible to get submitted records of form in beforeSave ? – CodeWithCoffee May 12 '15 at 06:17
  • First question: Yes. Second: It is possible and intended to get submitted records of forms. You can reach them via $this->attributeName. As in my second example. Date field comes from web form and only format of the date is changed in beforeSave(). – Ghokun May 12 '15 at 06:21
  • Does `yii\behaviors\TimestampBehavior;` is the nice way for this? – bumbumpaw Apr 01 '17 at 19:30
  • @bumbumpaw It looks like a better approach. I was using version 1 while answering that question. I see they added behaviours in version 2. – Ghokun Apr 02 '17 at 19:46