File modules/cms/model/ModelBase.class.php

Last commit: Tue Jun 14 01:11:09 2022 +0200	Jan Dankert	New: Support for ESI templates; some bugfixes while generating the 'insert' element type.
1 <?php 2 namespace cms\model; 3 4 use LogicException; 5 use util\text\TextMessage; 6 7 abstract class ModelBase 8 { 9 public function __construct() 10 { 11 } 12 13 /** 14 * All public properties of this object. 15 * @return array 16 */ 17 public function getProperties() 18 { 19 return get_object_vars( $this ); 20 } 21 22 /** 23 * The logical name of this object. 24 * 25 * @return string name 26 */ 27 public abstract function getName(); 28 29 /** 30 * Loading the instance from the database 31 */ 32 public abstract function load(); 33 34 /** 35 * Saving the instance to the database 36 */ 37 protected abstract function save(); 38 39 /** 40 * Adding the instance to the database 41 */ 42 protected abstract function add(); 43 44 /** 45 * Delete this instance 46 */ 47 public abstract function delete(); 48 49 /** 50 * Returns the unique ID of this object. 51 * 52 * @return int 53 */ 54 public abstract function getId(); 55 56 57 /** 58 * Is this instance already persistent in the database? 59 * 60 * @return bool 61 */ 62 public function isPersistent() 63 { 64 return (bool) $this->getId(); 65 } 66 67 68 /** 69 * Persist the object in the database 70 */ 71 public function persist() 72 { 73 if ( ! $this->isPersistent() ) 74 $this->add(); 75 76 $this->save(); 77 } 78 79 /** 80 * Updates the already existing object in the database 81 * 82 * @throws LogicException if not persistent 83 */ 84 public function update() 85 { 86 if ( ! $this->isPersistent() ) 87 throw new LogicException(TextMessage::create('Object ${0} is not persistent and cannot be updated', [ get_class($this).' '.$this->getName() ]) ); 88 89 $this->save(); 90 } 91 92 93 public function __toString() { 94 return $this->getId().':'.$this->getName(); 95 } 96 }
Download modules/cms/model/ModelBase.class.php
History Tue, 14 Jun 2022 01:11:09 +0200 Jan Dankert New: Support for ESI templates; some bugfixes while generating the 'insert' element type. Sat, 13 Feb 2021 00:41:43 +0100 Jan Dankert Fix: f*cking 'boolval()' is only available since PHP 5.5 :/ Wed, 18 Nov 2020 01:46:36 +0100 Jan Dankert Refactoring of model classes: New method persist() and some other cleanups. Fri, 2 Oct 2020 23:11:48 +0200 Jan Dankert Cleanup: No '.inputholder' any more, notices with links to objects. Tue, 29 Sep 2020 23:29:40 +0200 Jan Dankert Cleanup the 'moorweide'-theme. Sun, 23 Feb 2020 04:01:30 +0100 Jan Dankert Refactoring with Namespaces for the cms modules, part 1: moving.