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 | */ |
| 63 | PointedEar | 54 | public function __construct ( |
| 55 | array $data = null, array $mapping = null, $exclusiveMapping = false) |
||
| 27 | PointedEar | 56 | { |
| 63 | PointedEar | 57 | parent::__construct($data, $mapping, $exclusiveMapping); |
| 27 | PointedEar | 58 | } |
| 51 | PointedEar | 59 | |
| 60 | public function getPersistentTable () |
||
| 61 | { |
||
| 54 | PointedEar | 62 | $class = \get_class($this); |
| 63 | if (\is_string($class::$_persistentTable)) |
||
| 51 | PointedEar | 64 | { |
| 65 | /* Call setter to convert to Table */ |
||
| 54 | PointedEar | 66 | $this->setPersistentTable($class::$_persistentTable); |
| 51 | PointedEar | 67 | } |
| 68 | |||
| 54 | PointedEar | 69 | return $class::$_persistentTable; |
| 51 | PointedEar | 70 | } |
| 71 | |||
| 72 | public function setPersistentTable ($value) |
||
| 73 | { |
||
| 54 | PointedEar | 74 | $class = \get_class($this); |
| 51 | PointedEar | 75 | if ($value instanceof Table) |
| 76 | { |
||
| 54 | PointedEar | 77 | $class::$_persistentTable = $value; |
| 51 | PointedEar | 78 | } |
| 79 | else |
||
| 80 | { |
||
| 81 | $table = new $value(); |
||
| 52 | PointedEar | 82 | if (!($table instanceof Db\Table)) |
| 51 | PointedEar | 83 | { |
| 84 | throw new \InvalidArgumentException( |
||
| 52 | PointedEar | 85 | 'Parameter does not specify a subclass of \\PointedEars\\PHPX\\Table: ' |
| 86 | . $value |
||
| 51 | PointedEar | 87 | ); |
| 88 | } |
||
| 89 | |||
| 54 | PointedEar | 90 | $class::$_persistentTable = $table; |
| 51 | PointedEar | 91 | } |
| 92 | } |
||
| 93 | |||
| 27 | PointedEar | 94 | /** |
| 51 | PointedEar | 95 | * Returns an array for database queries containing the |
| 96 | * property values of this object, using the specified |
||
| 97 | * property-to-column mapping. |
||
| 98 | * |
||
| 99 | * @param array $propertyNames = null |
||
| 100 | * Names of the properties that should be included. |
||
| 101 | * The default is to include all persistent properties. |
||
| 102 | * @return array |
||
| 27 | PointedEar | 103 | */ |
| 51 | PointedEar | 104 | public function getPropertyArray (array $propertyNames = null) |
| 27 | PointedEar | 105 | { |
| 51 | PointedEar | 106 | $a = array(); |
| 107 | |||
| 108 | if ($propertyNames === null) |
||
| 109 | { |
||
| 55 | PointedEar | 110 | $class = \get_class($this); |
| 111 | $propertyNames = $class::$_persistentProperties; |
||
| 51 | PointedEar | 112 | } |
| 113 | |||
| 114 | foreach ($propertyNames as $propertyName => $columnName) |
||
| 115 | { |
||
| 116 | if (is_numeric($propertyName)) |
||
| 117 | { |
||
| 118 | $propertyName = $columnName; |
||
| 119 | } |
||
| 120 | |||
| 121 | $a[$columnName] = $this->$propertyName; |
||
| 122 | } |
||
| 123 | |||
| 124 | return $a; |
||
| 27 | PointedEar | 125 | } |
| 51 | PointedEar | 126 | |
| 127 | /** |
||
| 53 | PointedEar | 128 | * Finds the record for the model object in the table, fills |
| 51 | PointedEar | 129 | * the object with missing data, and returns the result. |
| 130 | * |
||
| 62 | PointedEar | 131 | * @param mixed $id = null |
| 132 | * The ID of the object to find. If <code>null</code> (default), |
||
| 133 | * the object is search for by its current ID. |
||
| 51 | PointedEar | 134 | * @see Table::find(Model) |
| 135 | * @return Model|null |
||
| 136 | * This object filled with missing data, or <code>null</code> |
||
| 137 | * if there is no data for this object |
||
| 65 | PointedEar | 138 | * @see Table::find(Model) |
| 51 | PointedEar | 139 | */ |
| 62 | PointedEar | 140 | public function find ($id = null) |
| 51 | PointedEar | 141 | { |
| 54 | PointedEar | 142 | $class = \get_class($this); |
| 65 | PointedEar | 143 | if ($id === null) |
| 144 | { |
||
| 145 | $id = $this->{$class::$_persistentId}; |
||
| 146 | } |
||
| 147 | |||
| 148 | $result = $this->persistentTable->find($id); |
||
| 62 | PointedEar | 149 | if ($id !== null) |
| 150 | { |
||
| 151 | $this->{$class::$_persistentId} = $id; |
||
| 152 | } |
||
| 153 | |||
| 55 | PointedEar | 154 | $result = $this->persistentTable->find($this->{$class::$_persistentId}); |
| 51 | PointedEar | 155 | if ($result) |
| 156 | { |
||
| 157 | return $this->map($result); |
||
| 158 | } |
||
| 159 | |||
| 160 | return null; |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 53 | PointedEar | 164 | * Saves the model object in the table |
| 51 | PointedEar | 165 | * |
| 166 | * @param array $propertyNames = null |
||
| 167 | * Names of the properties whose values should be saved |
||
| 168 | * in the database. The default is to save the values |
||
| 169 | * of all persistent properties. |
||
| 170 | * @return boolean |
||
| 62 | PointedEar | 171 | * The return value of |
| 172 | * <code>$this->persistentTable->updateOrInsert()</code>. |
||
| 51 | PointedEar | 173 | * @see Model::getPropertyArray() |
| 174 | * @see Table::updateOrInsert() |
||
| 175 | */ |
||
| 176 | public function save (array $propertyNames = null) |
||
| 177 | { |
||
| 53 | PointedEar | 178 | $table = $this->persistentTable; |
| 55 | PointedEar | 179 | $class = \get_class($this); |
| 180 | $idPropertyName = $class::$_persistentId; |
||
| 53 | PointedEar | 181 | |
| 182 | $result = $table->updateOrInsert( |
||
| 183 | $this->getPropertyArray($propertyNames), |
||
| 184 | array( |
||
| 185 | $table->id => $this->$idPropertyName |
||
| 51 | PointedEar | 186 | ) |
| 187 | ); |
||
| 53 | PointedEar | 188 | |
| 189 | if ($result && ($lastInsertId = $table->lastInsertId)) |
||
| 190 | { |
||
| 191 | $this->$idPropertyName = $lastInsertId; |
||
| 192 | } |
||
| 193 | |||
| 194 | return $result; |
||
| 51 | PointedEar | 195 | } |
| 196 | |||
| 197 | /** |
||
| 53 | PointedEar | 198 | * Inserts the model object into the table |
| 199 | * |
||
| 200 | * @param array $propertyNames = null |
||
| 201 | * Names of the properties whose values should be insert |
||
| 202 | * in the database. The default is to insert the values |
||
| 203 | * of all persistent properties. |
||
| 204 | * @return boolean |
||
| 205 | * @see Model::getPropertyArray() |
||
| 206 | * @see Table::insert() |
||
| 207 | */ |
||
| 208 | public function insert (array $propertyNames = null) |
||
| 209 | { |
||
| 210 | $table = $this->persistentTable; |
||
| 55 | PointedEar | 211 | $class = \get_class($this); |
| 212 | $idPropertyName = $class::$_persistentId; |
||
| 53 | PointedEar | 213 | |
| 214 | $result = $table->insert($this->getPropertyArray($propertyNames)); |
||
| 215 | |||
| 216 | if ($result && ($lastInsertId = $table->lastInsertId)) |
||
| 217 | { |
||
| 218 | $this->$idPropertyName = $lastInsertId; |
||
| 219 | } |
||
| 220 | |||
| 221 | return $result; |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 51 | PointedEar | 225 | * Deletes a model object from the <code>Table</code> |
| 226 | * |
||
| 227 | * @return bool |
||
| 228 | * @see Table::delete() |
||
| 229 | */ |
||
| 230 | public function delete () |
||
| 231 | { |
||
| 55 | PointedEar | 232 | $class = \get_class($this); |
| 233 | return $this->persistentTable->delete($this->{$class::$_persistentId}); |
||
| 51 | PointedEar | 234 | } |
| 27 | PointedEar | 235 | } |