CodeIgniter Forums

Full Version: Entities/Model - Validation and Calculation
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi

i have this inputs
PHP Code:
$inputs = [
        'revenue' => 100,
        'cost' => 50,
        'tax' => '20',
    ]; 



and i need this outputs to save it on database
PHP Code:
$outputs = [
        'revenue' => 100,
        'cost' => 50,
        'tax' => 20,
        'EBITDA' => 50,
        'netProfit' => 40,
    ]; 

i'm using Entities and Model
1- where i must validate fileds?
2- where i must caculate data to set netProfit and EBITDA for save in database?
1) One can place validation in the Entity so that validation is handled automatically, and is closely associated with the data it's validating (though, personally, because I find that in my typical usage, any given field frequently has several different conditions that sometimes do and sometimes don't apply, this started looking complicated... and I've resorted to having a separate 'validation class' that handles user input, which the Controller invokes separate to the Entity class (an approach that also has it's own inherent drawbacks)).

2) "Business rules" are typically supposed to be placed in the Model.