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 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示