doushi1974 2014-03-24 02:38
浏览 29
已采纳

无脂肪框架:我可以在保存之前设置模型来计算字段值吗?

Is there a way for the model to compute a field before save?

I have a schema something like this:

item_id
item_qty
item_price
linetotal

I'm trying to avoid this kind of code in my controller:

$model->linetotal = $f3->get('POST.item_qty') * $f3->get('POST.item_price');
$model->save();

Instead, I want to setup my model to compute the linetotal before save.

Perhaps something like the beforeSave callback in cakephp, or even better maybe I missed that I can set it up just like a Mapper virtual field on f3, except its a real field...

  • 写回答

1条回答 默认 最新

  • duanheye7423 2014-03-24 06:34
    关注

    What you're asking for has just been released in 3.2.2. DB mappers now come with the following hooks: beforeinsert, afterinsert, beforeupdate, afterupdate, beforeerase, aftererase and onload.

    So you could implement the linetotal calculation like this:

    class myModel extends \DB\SQL\Mapper {
    
      static function _beforeupdate($self,$pkeys) {
        $self->linetotal = $self->item_qty * $self->item_price;
      }
    
      function __construct() {
        $f3=\Base::instance();
        parent::construct($f3->get('DB'),'mytable');
        $this->beforeupdate(array(__CLASS__,'_beforeupdate'));
      }
    
    }
    

    But since the calculation is also relevant for INSERT statements, you'll also need to hook the beforeinsert event. You could use the same function to hook both events :

    class myModel extends \DB\SQL\Mapper {
    
      static function _linetotal($self,$pkeys) {
        $self->linetotal = $self->item_qty * $self->item_price;
      }
    
      function __construct() {
        $f3=\Base::instance();
        parent::construct($f3->get('DB'),'mytable');
        $this->beforeinsert(array(__CLASS__,'_linetotal'));
        $this->beforeupdate(array(__CLASS__,'_linetotal'));
      }
    
    }
    

    NB: an alternative way to implement the linetotal calculation would be to simply override the mapper set() method:

    class myModel extends \DB\SQL\Mapper {
    
      function set($key,$val) {
        parent::set($key,$val);
        if ($key=='item_qty' || $key=='item_price')
          $this->linetotal = $this->item_qty * $this->item_price;
      }
    
    }
    

    EDIT:

    I forgot a third alternative, which actually looks more appropriate in your case: virtual fields. In this case, the calculation is left to the DB engine. E.g:

    class myModel extends \DB\SQL\Mapper {
    
      function __construct(){
        $f3=\Base::instance();
        parent::construct($f3->get('DB'),'mytable');
        $this->linetotal = 'item_qty * item_price';
      }
    
    }
    

    PS: don't forget to remove the linetotal field from your database.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 hexo+github部署博客
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?