[英]Cakephp Best Practices
我對CakePHP中的最佳實踐有疑問!
讓我們假設以下情況:
在收據模型中我有代碼:
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;
}
我必須使用變量$ qtdade_cupom,$ cod_filial進行大量驗證,例如檢查有效數字。
我在哪里做數學?
我在模型中創建了一個方法
public function checkDigits()
要么
我創建一個行為來做到這一點?
要么
其他方案??
答案很簡單:取決於您的需求。
如果此數學由多個模型使用(不僅僅是針對此模型),請使用行為。
如果僅由此特定模型使用,請將其作為模型方法保留在其中。
如果數學涉及許多其他工具和類,最好將這個數學提取到/ Lib中可測試良好的庫中。
但正如我所說,取決於它究竟需要和用於什么。
CakePHP內置了模型驗證。
您還可以定義自定義驗證方法。
更多信息: http : //book.cakephp.org/2.0/en/models/data-validation.html#custom-validation-rules
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.