YII - 当你可以在Save()函数之前编码时,为什么要使用beforeSave()

时间:2023-01-14 10:50:38

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.

我知道YII中函数beforeSave()的功能,该函数用于执行某些操作,我们希望在保存数据之前执行该操作。

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 )

但是,就我们想要在我们的数据保存到数据库之前实现这一点,我们不能在save()调用之前直接编写这段代码( - > save()将记录存储到数据库)

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.

因此,我不确定为什么我们需要创建像beforeSave()这样的特定函数来执行我们需要在Save()调用之前触发的操作,当我们在save()行之前直接编写该代码时。

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.

有人可以解释一下吗?我已经搜索了很多,但是在每一页上,它都只重定向到beforeSave()函数的解释。

2 个解决方案

#1


Yii and other MVC frameworks have those kind of functions.

Yii和其他MVC框架具有这些功能。

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.

虽然您可以在控制器中编写“保存前”代码,但在save()函数之前 - 使用beforeSave()函数更有建议和有用。

Reason 1: The M in the MVC

原因1:MVC中的M.

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.

beforeSave与模型有关,因此拥有一个代码来处理模型文件中的模型属性(字段)而不是在控制器中拥有该代码更合乎逻辑。

Reason 2: Saving is for insert & update

原因2:保存用于插入和更新

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)

插入新记录时以及更新现有记录时使用save()。如果不使用beforeSave内置函数,在保存代码之前,您必须拥有2个“手册”实例。 (代码行“浪费”)

Reason 3: Saving a model from another controller

原因3:从另一个控制器保存模型

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.

如果你被要求扩展你的应用程序,现在你必须面对一个需要保存相同模型的新控制器(出于某种原因 - 只是一种可能的情况),你将不得不复制你的“保存之前” “该控制器的代码。如果您使用的是内置的beforeSave功能,则不会。

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?

总之,框架的主要目的是减少您需要编写的代码,同时保持逻辑(MVC分离)。虽然你可以做不同的事情,为什么不使用已经存在的东西?

#2


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.

我写了一次,所以我不必每次在该对象上调用save()时都要编写。

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();
}

#1


Yii and other MVC frameworks have those kind of functions.

Yii和其他MVC框架具有这些功能。

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.

虽然您可以在控制器中编写“保存前”代码,但在save()函数之前 - 使用beforeSave()函数更有建议和有用。

Reason 1: The M in the MVC

原因1:MVC中的M.

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.

beforeSave与模型有关,因此拥有一个代码来处理模型文件中的模型属性(字段)而不是在控制器中拥有该代码更合乎逻辑。

Reason 2: Saving is for insert & update

原因2:保存用于插入和更新

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)

插入新记录时以及更新现有记录时使用save()。如果不使用beforeSave内置函数,在保存代码之前,您必须拥有2个“手册”实例。 (代码行“浪费”)

Reason 3: Saving a model from another controller

原因3:从另一个控制器保存模型

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.

如果你被要求扩展你的应用程序,现在你必须面对一个需要保存相同模型的新控制器(出于某种原因 - 只是一种可能的情况),你将不得不复制你的“保存之前” “该控制器的代码。如果您使用的是内置的beforeSave功能,则不会。

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?

总之,框架的主要目的是减少您需要编写的代码,同时保持逻辑(MVC分离)。虽然你可以做不同的事情,为什么不使用已经存在的东西?

#2


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.

我写了一次,所以我不必每次在该对象上调用save()时都要编写。

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();
}