Reputation: 56
I have a question about best practices in CakePHP!
Let's imagine the following situation:
In a Receipt Model i have the code:
public function beforeValidate()
{
$this->data[$this->name]["client_id"] = CakeSession::read("Auth.User.id");
$this->data[$this->name]["date"] = date('Y-m-d H:i:s');
$receipt = $this->data[$this->name]["receipt"];
$cod_filial = substr($receipt, 0, 3);
$qtdade_cupom = substr($receipt, 12, 2);
$tipo_pagamento = substr($receipt, 14, 1);
$this->data[$this->name]["cod"] = $cod_filial;
$this->data[$this->name]["quantity"] = $qtdade_cupom;
$this->data[$this->name]["payment_type"] = $tipo_pagamento;
$this->data[$this->name]["is_valid"] = null;
return true;
}
I have to do a lot of verifications with the variables $qtdade_cupom, $cod_filial like check the valid digit.
Where do I do the maths??
I create a method inside the model like
public function checkDigits()
OR
I create a Behaviour to do this?
OR
Other solution??
Upvotes: 0
Views: 1026
Reputation: 333
CakePHP has model validation built in.
You can also define custom validation methods.
More Info: http://book.cakephp.org/2.0/en/models/data-validation.html#custom-validation-rules
Hope this helps.
Upvotes: 1
Reputation: 21743
The answer is easy: Depends on what you need.
If this math is used by more than one model (not just specific to this one), use a behavior.
If it is used only by this specific model, keep it as model method inside.
If the math involves a lot of other tools and classes, it might be best to extract this math into a well testable lib in /Lib.
But as I said, depends on what exactly it is needed and used for.
Upvotes: 1