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