Best way to handle dirty state in an ORM model

后端 未结 3 2011
星月不相逢
星月不相逢 2020-12-03 13:06

I don\'t want anyone saying \"you should not reinvent the wheel, use an open source ORM\"; I have an immediate requirement and cannot switch.

I\'m doing a l

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-03 13:35

    I would make a proxy to set for example:

    class BaseModel {
    
       protected function _set($attr, $value) {
          $current = $this->_get($attr);
          if($value !== $current) {
             $this->is_dirty = true;
          }
    
          $this->$attr = $value;
       }
    }
    

    Then each child class would implemnt its setter by calling _set() and never set the property directly. Further, you can always inject more class specific code into each sub class's _set and just call parent::set($attr, $processedValue) if needed. Then if you want to use magic methods you make those proxy to property method that proxies to _set. I suppose this isnt very POPO though.

提交回复
热议问题