Rev 62 | Rev 65 | 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 |
||
138 | */ |
||
62 | PointedEar | 139 | public function find ($id = null) |
51 | PointedEar | 140 | { |
54 | PointedEar | 141 | $class = \get_class($this); |
62 | PointedEar | 142 | if ($id !== null) |
143 | { |
||
144 | $this->{$class::$_persistentId} = $id; |
||
145 | } |
||
146 | |||
55 | PointedEar | 147 | $result = $this->persistentTable->find($this->{$class::$_persistentId}); |
51 | PointedEar | 148 | if ($result) |
149 | { |
||
150 | return $this->map($result); |
||
151 | } |
||
152 | |||
153 | return null; |
||
154 | } |
||
155 | |||
156 | /** |
||
53 | PointedEar | 157 | * Saves the model object in the table |
51 | PointedEar | 158 | * |
159 | * @param array $propertyNames = null |
||
160 | * Names of the properties whose values should be saved |
||
161 | * in the database. The default is to save the values |
||
162 | * of all persistent properties. |
||
163 | * @return boolean |
||
62 | PointedEar | 164 | * The return value of |
165 | * <code>$this->persistentTable->updateOrInsert()</code>. |
||
51 | PointedEar | 166 | * @see Model::getPropertyArray() |
167 | * @see Table::updateOrInsert() |
||
168 | */ |
||
169 | public function save (array $propertyNames = null) |
||
170 | { |
||
53 | PointedEar | 171 | $table = $this->persistentTable; |
55 | PointedEar | 172 | $class = \get_class($this); |
173 | $idPropertyName = $class::$_persistentId; |
||
53 | PointedEar | 174 | |
175 | $result = $table->updateOrInsert( |
||
176 | $this->getPropertyArray($propertyNames), |
||
177 | array( |
||
178 | $table->id => $this->$idPropertyName |
||
51 | PointedEar | 179 | ) |
180 | ); |
||
53 | PointedEar | 181 | |
182 | if ($result && ($lastInsertId = $table->lastInsertId)) |
||
183 | { |
||
184 | $this->$idPropertyName = $lastInsertId; |
||
185 | } |
||
186 | |||
187 | return $result; |
||
51 | PointedEar | 188 | } |
189 | |||
190 | /** |
||
53 | PointedEar | 191 | * Inserts the model object into the table |
192 | * |
||
193 | * @param array $propertyNames = null |
||
194 | * Names of the properties whose values should be insert |
||
195 | * in the database. The default is to insert the values |
||
196 | * of all persistent properties. |
||
197 | * @return boolean |
||
198 | * @see Model::getPropertyArray() |
||
199 | * @see Table::insert() |
||
200 | */ |
||
201 | public function insert (array $propertyNames = null) |
||
202 | { |
||
203 | $table = $this->persistentTable; |
||
55 | PointedEar | 204 | $class = \get_class($this); |
205 | $idPropertyName = $class::$_persistentId; |
||
53 | PointedEar | 206 | |
207 | $result = $table->insert($this->getPropertyArray($propertyNames)); |
||
208 | |||
209 | if ($result && ($lastInsertId = $table->lastInsertId)) |
||
210 | { |
||
211 | $this->$idPropertyName = $lastInsertId; |
||
212 | } |
||
213 | |||
214 | return $result; |
||
215 | } |
||
216 | |||
217 | /** |
||
51 | PointedEar | 218 | * Deletes a model object from the <code>Table</code> |
219 | * |
||
220 | * @return bool |
||
221 | * @see Table::delete() |
||
222 | */ |
||
223 | public function delete () |
||
224 | { |
||
55 | PointedEar | 225 | $class = \get_class($this); |
226 | return $this->persistentTable->delete($this->{$class::$_persistentId}); |
||
51 | PointedEar | 227 | } |
27 | PointedEar | 228 | } |