php - YII - Why to use beforeSave() when you can code before Save() function -


i aware of functionality of function beforesave() in yii function used perform something, want perform before our data saved.

however, far want implement before our data got save database, can't directly write code before save() calling (-> save () storing out records database )

hence, not sure why need create specific function beforesave () perform action need fire before save() called, when directly write code before save() line well.

can please explain ? have searched lot reason of on every page, redirect explanation of beforesave() function only.

yii , other mvc frameworks have kind of functions.

while can write "before save" code in controller, prior save() function - it's more recommended , useful use beforesave() function.

reason 1: m in mvc

the beforesave relates model, more logical have code handles model's properties (fields) in model's file rather having code in controller.

reason 2: saving insert & update

you use save() when insert new record , when update existing record. without using beforesave built-in function, you'll have have 2 instances of "manual" before save code. ("waste" of code lines)

reason 3: saving model controller

what if you'll asked expand application , you'd have face new controller need save same model (from reason - possible scenario) - you'll have copy "before save" code controller. while if you're using built-in beforesave function - don't.

in conclusion, main purpose of frameworks reduce code need write while keeping logical (mvc separation). while can things differently, why not using what's exists?


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -