dongmacuo1193 2013-10-16 21:18 采纳率: 0%
浏览 20

Cakephp最佳实践

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??

  • 写回答

2条回答 默认 最新

  • dpkrh2444 2013-10-16 21:36
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题