Rev 55 | Rev 63 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line | 
|---|---|---|---|
| 27 | PointedEar | 1 | <?php | 
        
| 2 | |||
| 52 | PointedEar | 3 | namespace PointedEars\PHPX;  | 
        
| 29 | PointedEar | 4 | |
| 27 | PointedEar | 5 | /** | 
        
| 51 | PointedEar | 6 | * Abstract model class for Object-Relational Mapping | 
        
| 27 | PointedEar | 7 | * | 
        
| 51 | PointedEar | 8 | * Provides simple mapping of a model object to records of | 
        
| 9 | * a table of a relational database. | 
        ||
| 27 | PointedEar | 10 | * | 
        
| 62 | PointedEar | 11 | * @property Db\Table $persistentTable | 
        
| 27 | PointedEar | 12 | * @author Thomas Lahn | 
        
| 13 | */ | 
        ||
| 52 | PointedEar | 14 | abstract class Model extends \PointedEars\PHPX\AbstractModel  | 
        
| 27 | PointedEar | 15 | { | 
        
| 16 |   /** | 
        ||
| 51 | PointedEar | 17 |    * The <code>Table</code> for instances of this model | 
        
| 18 |    * | 
        ||
| 19 |    * @type Table|string | 
        ||
| 27 | PointedEar | 20 |    */ | 
        
| 54 | PointedEar | 21 | protected static $_persistentTable;  | 
        
| 51 | PointedEar | 22 | |
| 27 | PointedEar | 23 |   /** | 
        
| 51 | PointedEar | 24 |    * The name(s) of the property or properties whose value(s) | 
        
| 25 |    * identify this object in the <code>Table</code>.  They are | 
        ||
| 26 |    * used for comparing against the primary key column(s) of | 
        ||
| 27 |    * the <code>Table</code>. | 
        ||
| 28 |    * | 
        ||
| 29 |    * @type string|array[string] | 
        ||
| 30 |    */ | 
        ||
| 55 | PointedEar | 31 | protected static $_persistentId = 'id';  | 
        
| 51 | PointedEar | 32 | |
| 33 |   /** | 
        ||
| 34 |    * The names of the properties that should be used in database | 
        ||
| 35 |    * queries, and their mapping to the columns of | 
        ||
| 36 |    * the <code>Table</code>, if specified (keys are property names, | 
        ||
| 37 |    * values are column names, or both if the key is numeric). | 
        ||
| 38 |    * | 
        ||
| 53 | PointedEar | 39 |    * NOTE: It should not be necessary to include the | 
        
| 40 |    * <code>persistentId</code> property value here.  If an object | 
        ||
| 41 |    * is not in the database, it should be assigned an ID | 
        ||
| 42 |    * automatically when saved; if it is in the database, | 
        ||
| 43 |    * you already have its ID as you searched by it. | 
        ||
| 44 |    * | 
        ||
| 51 | PointedEar | 45 |    * @type array | 
        
| 46 |    */ | 
        ||
| 55 | PointedEar | 47 | protected static $_persistentProperties = array();  | 
        
| 51 | PointedEar | 48 | |
| 49 |         /** | 
        ||
| 27 | PointedEar | 50 |    * Creates a new model object | 
        
| 51 |    * | 
        ||
| 51 | PointedEar | 52 |    * @see AbstractModel::__construct() | 
        
| 27 | PointedEar | 53 |    */ | 
        
| 51 | PointedEar | 54 | public function __construct (array $data = null, array $mapping = null)  | 
        
| 27 | PointedEar | 55 |   { | 
        
| 56 | parent::__construct($data, $mapping);  | 
        ||
| 57 |   } | 
        ||
| 51 | PointedEar | 58 | |
| 59 | public function getPersistentTable ()  | 
        ||
| 60 |   { | 
        ||
| 54 | PointedEar | 61 | $class = \get_class($this);  | 
        
| 62 | if (\is_string($class::$_persistentTable))  | 
        ||
| 51 | PointedEar | 63 |         { | 
        
| 64 |                 /* Call setter to convert to Table */ | 
        ||
| 54 | PointedEar | 65 | $this->setPersistentTable($class::$_persistentTable);  | 
        
| 51 | PointedEar | 66 |         } | 
        
| 67 | |||
| 54 | PointedEar | 68 | return $class::$_persistentTable;  | 
        
| 51 | PointedEar | 69 |   } | 
        
| 70 | |||
| 71 | public function setPersistentTable ($value)  | 
        ||
| 72 |   { | 
        ||
| 54 | PointedEar | 73 | $class = \get_class($this);  | 
        
| 51 | PointedEar | 74 | if ($value instanceof Table)  | 
        
| 75 |         { | 
        ||
| 54 | PointedEar | 76 | $class::$_persistentTable = $value;  | 
        
| 51 | PointedEar | 77 |         } | 
        
| 78 |         else | 
        ||
| 79 |         { | 
        ||
| 80 | $table = new $value();  | 
        ||
| 52 | PointedEar | 81 | if (!($table instanceof Db\Table))  | 
        
| 51 | PointedEar | 82 |                 { | 
        
| 83 | throw new \InvalidArgumentException(  | 
        ||
| 52 | PointedEar | 84 |                                 'Parameter does not specify a subclass of \\PointedEars\\PHPX\\Table: ' | 
        
| 85 | . $value  | 
        ||
| 51 | PointedEar | 86 | );  | 
        
| 87 |                 } | 
        ||
| 88 | |||
| 54 | PointedEar | 89 | $class::$_persistentTable = $table;  | 
        
| 51 | PointedEar | 90 |         } | 
        
| 91 |   } | 
        ||
| 92 | |||
| 27 | PointedEar | 93 |   /** | 
        
| 51 | PointedEar | 94 |    * Returns an array for database queries containing the | 
        
| 95 |    * property values of this object, using the specified | 
        ||
| 96 |    * property-to-column mapping. | 
        ||
| 97 |    * | 
        ||
| 98 |    * @param array $propertyNames = null | 
        ||
| 99 |    *   Names of the properties that should be included. | 
        ||
| 100 |    *   The default is to include all persistent properties. | 
        ||
| 101 |    * @return array | 
        ||
| 27 | PointedEar | 102 |    */ | 
        
| 51 | PointedEar | 103 | public function getPropertyArray (array $propertyNames = null)  | 
        
| 27 | PointedEar | 104 |   { | 
        
| 51 | PointedEar | 105 | $a = array();  | 
        
| 106 | |||
| 107 | if ($propertyNames === null)  | 
        ||
| 108 |         { | 
        ||
| 55 | PointedEar | 109 | $class = \get_class($this);  | 
        
| 110 | $propertyNames = $class::$_persistentProperties;  | 
        ||
| 51 | PointedEar | 111 |         } | 
        
| 112 | |||
| 113 | foreach ($propertyNames as $propertyName => $columnName)  | 
        ||
| 114 |         { | 
        ||
| 115 | if (is_numeric($propertyName))  | 
        ||
| 116 |                 { | 
        ||
| 117 | $propertyName = $columnName;  | 
        ||
| 118 |                 } | 
        ||
| 119 | |||
| 120 | $a[$columnName] = $this->$propertyName;  | 
        ||
| 121 |         } | 
        ||
| 122 | |||
| 123 | return $a;  | 
        ||
| 27 | PointedEar | 124 |   } | 
        
| 51 | PointedEar | 125 | |
| 126 |   /** | 
        ||
| 53 | PointedEar | 127 |    * Finds the record for the model object in the table, fills | 
        
| 51 | PointedEar | 128 |    * the object with missing data, and returns the result. | 
        
| 129 |    * | 
        ||
| 62 | PointedEar | 130 |    * @param mixed $id = null | 
        
| 131 |    *   The ID of the object to find.  If <code>null</code> (default), | 
        ||
| 132 |    *   the object is search for by its current ID. | 
        ||
| 51 | PointedEar | 133 |    * @see Table::find(Model) | 
        
| 134 |    * @return Model|null | 
        ||
| 135 |    *   This object filled with missing data, or <code>null</code> | 
        ||
| 136 |    *   if there is no data for this object | 
        ||
| 137 |    */ | 
        ||
| 62 | PointedEar | 138 | public function find ($id = null)  | 
        
| 51 | PointedEar | 139 |   { | 
        
| 54 | PointedEar | 140 | $class = \get_class($this);  | 
        
| 62 | PointedEar | 141 | if ($id !== null)  | 
        
| 142 |     { | 
        ||
| 143 | $this->{$class::$_persistentId} = $id;  | 
        ||
| 144 |     } | 
        ||
| 145 | |||
| 55 | PointedEar | 146 | $result = $this->persistentTable->find($this->{$class::$_persistentId});  | 
        
| 51 | PointedEar | 147 | if ($result)  | 
        
| 148 |     { | 
        ||
| 149 | return $this->map($result);  | 
        ||
| 150 |     } | 
        ||
| 151 | |||
| 152 | return null;  | 
        ||
| 153 |   } | 
        ||
| 154 | |||
| 155 |   /** | 
        ||
| 53 | PointedEar | 156 |    * Saves the model object in the table | 
        
| 51 | PointedEar | 157 |    * | 
        
| 158 |    * @param array $propertyNames = null | 
        ||
| 159 |    *   Names of the properties whose values should be saved | 
        ||
| 160 |    *   in the database.  The default is to save the values | 
        ||
| 161 |    *   of all persistent properties. | 
        ||
| 162 |    * @return boolean | 
        ||
| 62 | PointedEar | 163 |    *   The return value of | 
        
| 164 |    *   <code>$this->persistentTable->updateOrInsert()</code>. | 
        ||
| 51 | PointedEar | 165 |    * @see Model::getPropertyArray() | 
        
| 166 |    * @see Table::updateOrInsert() | 
        ||
| 167 |    */ | 
        ||
| 168 | public function save (array $propertyNames = null)  | 
        ||
| 169 |   { | 
        ||
| 53 | PointedEar | 170 | $table = $this->persistentTable;  | 
        
| 55 | PointedEar | 171 | $class = \get_class($this);  | 
        
| 172 | $idPropertyName = $class::$_persistentId;  | 
        ||
| 53 | PointedEar | 173 | |
| 174 | $result = $table->updateOrInsert(  | 
        ||
| 175 | $this->getPropertyArray($propertyNames),  | 
        ||
| 176 | array(  | 
        ||
| 177 | $table->id => $this->$idPropertyName  | 
        ||
| 51 | PointedEar | 178 |                 ) | 
        
| 179 | );  | 
        ||
| 53 | PointedEar | 180 | |
| 181 | if ($result && ($lastInsertId = $table->lastInsertId))  | 
        ||
| 182 |         { | 
        ||
| 183 | $this->$idPropertyName = $lastInsertId;  | 
        ||
| 184 |         } | 
        ||
| 185 | |||
| 186 | return $result;  | 
        ||
| 51 | PointedEar | 187 |   } | 
        
| 188 | |||
| 189 |   /** | 
        ||
| 53 | PointedEar | 190 |    * Inserts the model object into the table | 
        
| 191 |    * | 
        ||
| 192 |    * @param array $propertyNames = null | 
        ||
| 193 |    *   Names of the properties whose values should be insert | 
        ||
| 194 |    *   in the database.  The default is to insert the values | 
        ||
| 195 |    *   of all persistent properties. | 
        ||
| 196 |    * @return boolean | 
        ||
| 197 |    * @see Model::getPropertyArray() | 
        ||
| 198 |    * @see Table::insert() | 
        ||
| 199 |    */ | 
        ||
| 200 | public function insert (array $propertyNames = null)  | 
        ||
| 201 |   { | 
        ||
| 202 | $table = $this->persistentTable;  | 
        ||
| 55 | PointedEar | 203 | $class = \get_class($this);  | 
        
| 204 | $idPropertyName = $class::$_persistentId;  | 
        ||
| 53 | PointedEar | 205 | |
| 206 | $result = $table->insert($this->getPropertyArray($propertyNames));  | 
        ||
| 207 | |||
| 208 | if ($result && ($lastInsertId = $table->lastInsertId))  | 
        ||
| 209 |         { | 
        ||
| 210 | $this->$idPropertyName = $lastInsertId;  | 
        ||
| 211 |         } | 
        ||
| 212 | |||
| 213 | return $result;  | 
        ||
| 214 |   } | 
        ||
| 215 | |||
| 216 |   /** | 
        ||
| 51 | PointedEar | 217 |    * Deletes a model object from the <code>Table</code> | 
        
| 218 |    * | 
        ||
| 219 |    * @return bool | 
        ||
| 220 |    * @see Table::delete() | 
        ||
| 221 |    */ | 
        ||
| 222 | public function delete ()  | 
        ||
| 223 |   { | 
        ||
| 55 | PointedEar | 224 | $class = \get_class($this);  | 
        
| 225 | return $this->persistentTable->delete($this->{$class::$_persistentId});  | 
        ||
| 51 | PointedEar | 226 |   } | 
        
| 27 | PointedEar | 227 | } |