.htaccess
1 year ago
ArrayData.php
1 year ago
DBObject.php
1 year ago
Engine.php
1 year ago
Mysqldump.php
5 months ago
ReflectionObject.php
1 year ago
SleekStore.php
1 year ago
index.html
1 year ago
web.config
1 year ago
DBObject.php
122 lines
| 1 | <?php |
| 2 | |
| 3 | namespace JetBackup\Data; |
| 4 | |
| 5 | use JetBackup\Exception\DBException; |
| 6 | use JetBackup\JetBackup; |
| 7 | use SleekDB\Exceptions\InvalidArgumentException; |
| 8 | use SleekDB\Exceptions\IOException; |
| 9 | |
| 10 | if (!defined( '__JETBACKUP__')) die('Direct access is not allowed'); |
| 11 | |
| 12 | class DBObject extends ArrayData { |
| 13 | |
| 14 | private array $_data=[]; |
| 15 | private array $_find=[]; |
| 16 | private SleekStore $_db; |
| 17 | |
| 18 | /** |
| 19 | * @throws DBException |
| 20 | * @throws InvalidArgumentException |
| 21 | * @throws IOException |
| 22 | */ |
| 23 | public function __construct($collection) { |
| 24 | $this->_db = new SleekStore($collection); |
| 25 | } |
| 26 | |
| 27 | public function getDB():SleekStore { |
| 28 | return $this->_db; |
| 29 | } |
| 30 | |
| 31 | public function margeData($data=[]):void { |
| 32 | parent::margeData($data); |
| 33 | $this->_data = array_merge($this->_data, $data); |
| 34 | } |
| 35 | |
| 36 | public function setData($data=[]):void { |
| 37 | parent::setData($data); |
| 38 | $this->_data = []; |
| 39 | } |
| 40 | |
| 41 | public function set($key, $value):void { |
| 42 | parent::set($key, $value); |
| 43 | $this->_data[$key] = $value; |
| 44 | } |
| 45 | |
| 46 | private function _save_new():void { |
| 47 | if(!($data = $this->getData())) return; |
| 48 | unset($data[JetBackup::ID_FIELD]); |
| 49 | $this->getDB()->clearCache(); |
| 50 | $ret = $this->getDB()->insert($data); |
| 51 | $this->setId($ret[JetBackup::ID_FIELD]); |
| 52 | $this->setFind([[JetBackup::ID_FIELD, '=', $ret[JetBackup::ID_FIELD]]]); |
| 53 | $this->_data = []; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @throws InvalidArgumentException |
| 58 | */ |
| 59 | public function delete():void { |
| 60 | if(!$this->getId()) return; |
| 61 | $this->getDB()->clearCache(); |
| 62 | $this->getDB()->deleteById($this->getId()); |
| 63 | $this->setData(); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * @throws IOException |
| 68 | * @throws InvalidArgumentException |
| 69 | */ |
| 70 | public function save():void { |
| 71 | if($this->getId()) $this->setFind([[JetBackup::ID_FIELD, '=', $this->getId()]]); |
| 72 | |
| 73 | if(!$this->_find) { |
| 74 | $this->_save_new(); |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | if(!$this->_data) return; |
| 79 | $this->getDB()->clearCache(); |
| 80 | $this->getDB()->createQueryBuilder()->where($this->_find)->getQuery()->update($this->_data); |
| 81 | $this->_data = []; |
| 82 | } |
| 83 | |
| 84 | public function setId(int $id):void { $this->set(JetBackup::ID_FIELD, $id); } |
| 85 | public function getId():int { return (int) $this->get(JetBackup::ID_FIELD, 0); } |
| 86 | |
| 87 | public function setFind(array $find):void { $this->_find = $find; } |
| 88 | |
| 89 | /** |
| 90 | * @throws InvalidArgumentException |
| 91 | */ |
| 92 | protected function _loadById(int $id):void { |
| 93 | $data = $this->getDB()->findById($id); |
| 94 | if(!$data) return; |
| 95 | $this->setData($data); |
| 96 | //$this->_load([[JetBackup::ID_FIELD, '=', $id]]); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @throws InvalidArgumentException |
| 101 | * @throws IOException |
| 102 | */ |
| 103 | public function load():void { |
| 104 | $query = $this->_db->createQueryBuilder(); |
| 105 | if($this->_find) foreach($this->_find as $where) $query->where($where); |
| 106 | $data = $query->getQuery()->first(); |
| 107 | if(!$data) return; |
| 108 | $this->setData($data); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * @throws InvalidArgumentException |
| 113 | * @throws IOException |
| 114 | */ |
| 115 | protected function _load(array $find):void { |
| 116 | $this->setFind($find); |
| 117 | $this->load(); |
| 118 | if($this->getId()) $this->setFind([[JetBackup::ID_FIELD, '=', $this->getId()]]); |
| 119 | else $this->setFind([]); |
| 120 | } |
| 121 | |
| 122 | } |