1<?php 2 3/** 4 * Class CachedActiveRecord 5 * @author Fabian Schmid <fs@studer-raimann.ch> 6 */ 7abstract class CachedActiveRecord extends ActiveRecord implements arStorageInterface 8{ 9 /** 10 * @var string 11 */ 12 private $_hash = ''; 13 14 /** 15 * @return ilGlobalCache 16 */ 17 abstract public function getCache() : ilGlobalCache; 18 19 /** 20 * @return string 21 */ 22 final public function getCacheIdentifier() : string 23 { 24 if ($this->getArFieldList()->getPrimaryField()) { 25 return ($this->getConnectorContainerName() . "_" . $this->getPrimaryFieldValue()); 26 } 27 28 return ""; 29 } 30 31 public function getTTL() : int 32 { 33 return 60; 34 } 35 36 /** 37 * @inheritDoc 38 */ 39 public function __construct($primary_key = 0, arConnector $connector = null) 40 { 41 if (is_null($connector)) { 42 $connector = new arConnectorDB(); 43 } 44 45 $connector = new arConnectorCache($connector); 46 arConnectorMap::register($this, $connector); 47 parent::__construct($primary_key, $connector); 48 } 49 50 public function afterObjectLoad() 51 { 52 parent::afterObjectLoad(); 53 $this->_hash = $this->buildHash(); 54 } 55 56 private function buildHash() : string 57 { 58 $hashing = []; 59 foreach ($this->getArFieldList()->getFields() as $field) { 60 $name = $field->getName(); 61 $hashing[$name] = $this->{$name}; 62 } 63 return md5(serialize($hashing)); 64 } 65 66 public function storeObjectToCache() 67 { 68 parent::storeObjectToCache(); 69 } 70 71 /** 72 * @inheritDoc 73 */ 74 public function buildFromArray(array $array) 75 { 76 return parent::buildFromArray($array); 77 } 78 79 public function store() 80 { 81 parent::store(); 82 } 83 84 public function save() 85 { 86 parent::save(); 87 } 88 89 public function create() 90 { 91 $this->getCache()->flush(); 92 parent::create(); 93 } 94 95 /** 96 * @inheritDoc 97 */ 98 public function copy($new_id = 0) 99 { 100 $this->getCache()->flush(); 101 return parent::copy($new_id); 102 } 103 104 public function read() 105 { 106 parent::read(); 107 } 108 109 public function update() 110 { 111 if ($this->buildHash() !== $this->_hash) { 112 $this->getCache()->flush(); 113 parent::update(); 114 } 115 } 116 117 public function delete() 118 { 119 $this->getCache()->flush(); 120 parent::delete(); // TODO: Change the autogenerated stub 121 } 122 123 /** 124 * @inheritDoc 125 */ 126 public static function find($primary_key, array $add_constructor_args = array()) 127 { 128 return parent::find($primary_key, $add_constructor_args); 129 } 130 131 /** 132 * @inheritDoc 133 */ 134 public static function connector(arConnector $connector) 135 { 136 return parent::connector($connector); // TODO: Change the autogenerated stub 137 } 138} 139