@@ -27,170 +27,170 @@ discard block |
||
27 | 27 | */ |
28 | 28 | class DbRow |
29 | 29 | { |
30 | - /** |
|
31 | - * The service this object is bound to. |
|
32 | - * |
|
33 | - * @var TDBMService |
|
34 | - */ |
|
35 | - protected $tdbmService; |
|
36 | - |
|
37 | - /** |
|
38 | - * The object containing this db row. |
|
39 | - * |
|
40 | - * @var AbstractTDBMObject |
|
41 | - */ |
|
42 | - private $object; |
|
43 | - |
|
44 | - /** |
|
45 | - * The name of the table the object if issued from. |
|
46 | - * |
|
47 | - * @var string |
|
48 | - */ |
|
49 | - private $dbTableName; |
|
50 | - |
|
51 | - /** |
|
52 | - * The array of columns returned from database. |
|
53 | - * |
|
54 | - * @var array |
|
55 | - */ |
|
56 | - private $dbRow = array(); |
|
57 | - |
|
58 | - /** |
|
59 | - * @var AbstractTDBMObject[] |
|
60 | - */ |
|
61 | - private $references = array(); |
|
62 | - |
|
63 | - /** |
|
64 | - * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
65 | - * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
66 | - * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
67 | - * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
68 | - * |
|
69 | - * @var string |
|
70 | - */ |
|
71 | - private $status; |
|
72 | - |
|
73 | - /** |
|
74 | - * The values of the primary key. |
|
75 | - * This is set when the object is in "loaded" state. |
|
76 | - * |
|
77 | - * @var array An array of column => value |
|
78 | - */ |
|
79 | - private $primaryKeys; |
|
80 | - |
|
81 | - /** |
|
82 | - * You should never call the constructor directly. Instead, you should use the |
|
83 | - * TDBMService class that will create TDBMObjects for you. |
|
84 | - * |
|
85 | - * Used with id!=false when we want to retrieve an existing object |
|
86 | - * and id==false if we want a new object |
|
87 | - * |
|
88 | - * @param AbstractTDBMObject $object The object containing this db row. |
|
89 | - * @param string $table_name |
|
90 | - * @param array $primaryKeys |
|
91 | - * @param TDBMService $tdbmService |
|
92 | - * |
|
93 | - * @throws TDBMException |
|
94 | - * @throws TDBMInvalidOperationException |
|
95 | - */ |
|
96 | - public function __construct(AbstractTDBMObject $object, $table_name, array $primaryKeys = array(), TDBMService $tdbmService = null, array $dbRow = array()) |
|
97 | - { |
|
98 | - $this->object = $object; |
|
99 | - $this->dbTableName = $table_name; |
|
100 | - |
|
101 | - $this->status = TDBMObjectStateEnum::STATE_DETACHED; |
|
102 | - |
|
103 | - if ($tdbmService === null) { |
|
104 | - if (!empty($primaryKeys)) { |
|
105 | - throw new TDBMException('You cannot pass an id to the DbRow constructor without passing also a TDBMService.'); |
|
106 | - } |
|
107 | - } else { |
|
108 | - $this->tdbmService = $tdbmService; |
|
109 | - |
|
110 | - if (!empty($primaryKeys)) { |
|
111 | - $this->_setPrimaryKeys($primaryKeys); |
|
112 | - if (!empty($dbRow)) { |
|
113 | - $this->dbRow = $dbRow; |
|
114 | - $this->status = TDBMObjectStateEnum::STATE_LOADED; |
|
115 | - } else { |
|
116 | - $this->status = TDBMObjectStateEnum::STATE_NOT_LOADED; |
|
117 | - } |
|
118 | - $tdbmService->_addToCache($this); |
|
119 | - } else { |
|
120 | - $this->status = TDBMObjectStateEnum::STATE_NEW; |
|
121 | - $this->tdbmService->_addToToSaveObjectList($this); |
|
122 | - } |
|
123 | - } |
|
124 | - } |
|
125 | - |
|
126 | - public function _attach(TDBMService $tdbmService) |
|
127 | - { |
|
128 | - if ($this->status !== TDBMObjectStateEnum::STATE_DETACHED) { |
|
129 | - throw new TDBMInvalidOperationException('Cannot attach an object that is already attached to TDBM.'); |
|
130 | - } |
|
131 | - $this->tdbmService = $tdbmService; |
|
132 | - $this->status = TDBMObjectStateEnum::STATE_NEW; |
|
133 | - $this->tdbmService->_addToToSaveObjectList($this); |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * Sets the state of the TDBM Object |
|
138 | - * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
139 | - * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
140 | - * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
141 | - * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
142 | - * |
|
143 | - * @param string $state |
|
144 | - */ |
|
145 | - public function _setStatus($state) |
|
146 | - { |
|
147 | - $this->status = $state; |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * This is an internal method. You should not call this method yourself. The TDBM library will do it for you. |
|
152 | - * If the object is in state 'not loaded', this method performs a query in database to load the object. |
|
153 | - * |
|
154 | - * A TDBMException is thrown is no object can be retrieved (for instance, if the primary key specified |
|
155 | - * cannot be found). |
|
156 | - */ |
|
157 | - public function _dbLoadIfNotLoaded() |
|
158 | - { |
|
159 | - if ($this->status == TDBMObjectStateEnum::STATE_NOT_LOADED) { |
|
160 | - $connection = $this->tdbmService->getConnection(); |
|
161 | - |
|
162 | - /// buildFilterFromFilterBag($filter_bag) |
|
163 | - list($sql_where, $parameters) = $this->tdbmService->buildFilterFromFilterBag($this->primaryKeys); |
|
164 | - |
|
165 | - $sql = 'SELECT * FROM '.$connection->quoteIdentifier($this->dbTableName).' WHERE '.$sql_where; |
|
166 | - $result = $connection->executeQuery($sql, $parameters); |
|
167 | - |
|
168 | - if ($result->rowCount() === 0) { |
|
169 | - throw new TDBMException("Could not retrieve object from table \"$this->dbTableName\" using filter \"\"."); |
|
170 | - } |
|
171 | - |
|
172 | - $row = $result->fetch(\PDO::FETCH_ASSOC); |
|
173 | - |
|
174 | - $this->dbRow = []; |
|
175 | - $types = $this->tdbmService->_getColumnTypesForTable($this->dbTableName); |
|
176 | - |
|
177 | - foreach ($row as $key => $value) { |
|
178 | - $this->dbRow[$key] = $types[$key]->convertToPHPValue($value, $connection->getDatabasePlatform()); |
|
179 | - } |
|
180 | - |
|
181 | - $result->closeCursor(); |
|
182 | - |
|
183 | - $this->status = TDBMObjectStateEnum::STATE_LOADED; |
|
184 | - } |
|
185 | - } |
|
186 | - |
|
187 | - public function get($var) |
|
188 | - { |
|
189 | - $this->_dbLoadIfNotLoaded(); |
|
190 | - |
|
191 | - // Let's first check if the key exist. |
|
192 | - if (!isset($this->dbRow[$var])) { |
|
193 | - /* |
|
30 | + /** |
|
31 | + * The service this object is bound to. |
|
32 | + * |
|
33 | + * @var TDBMService |
|
34 | + */ |
|
35 | + protected $tdbmService; |
|
36 | + |
|
37 | + /** |
|
38 | + * The object containing this db row. |
|
39 | + * |
|
40 | + * @var AbstractTDBMObject |
|
41 | + */ |
|
42 | + private $object; |
|
43 | + |
|
44 | + /** |
|
45 | + * The name of the table the object if issued from. |
|
46 | + * |
|
47 | + * @var string |
|
48 | + */ |
|
49 | + private $dbTableName; |
|
50 | + |
|
51 | + /** |
|
52 | + * The array of columns returned from database. |
|
53 | + * |
|
54 | + * @var array |
|
55 | + */ |
|
56 | + private $dbRow = array(); |
|
57 | + |
|
58 | + /** |
|
59 | + * @var AbstractTDBMObject[] |
|
60 | + */ |
|
61 | + private $references = array(); |
|
62 | + |
|
63 | + /** |
|
64 | + * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
65 | + * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
66 | + * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
67 | + * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
68 | + * |
|
69 | + * @var string |
|
70 | + */ |
|
71 | + private $status; |
|
72 | + |
|
73 | + /** |
|
74 | + * The values of the primary key. |
|
75 | + * This is set when the object is in "loaded" state. |
|
76 | + * |
|
77 | + * @var array An array of column => value |
|
78 | + */ |
|
79 | + private $primaryKeys; |
|
80 | + |
|
81 | + /** |
|
82 | + * You should never call the constructor directly. Instead, you should use the |
|
83 | + * TDBMService class that will create TDBMObjects for you. |
|
84 | + * |
|
85 | + * Used with id!=false when we want to retrieve an existing object |
|
86 | + * and id==false if we want a new object |
|
87 | + * |
|
88 | + * @param AbstractTDBMObject $object The object containing this db row. |
|
89 | + * @param string $table_name |
|
90 | + * @param array $primaryKeys |
|
91 | + * @param TDBMService $tdbmService |
|
92 | + * |
|
93 | + * @throws TDBMException |
|
94 | + * @throws TDBMInvalidOperationException |
|
95 | + */ |
|
96 | + public function __construct(AbstractTDBMObject $object, $table_name, array $primaryKeys = array(), TDBMService $tdbmService = null, array $dbRow = array()) |
|
97 | + { |
|
98 | + $this->object = $object; |
|
99 | + $this->dbTableName = $table_name; |
|
100 | + |
|
101 | + $this->status = TDBMObjectStateEnum::STATE_DETACHED; |
|
102 | + |
|
103 | + if ($tdbmService === null) { |
|
104 | + if (!empty($primaryKeys)) { |
|
105 | + throw new TDBMException('You cannot pass an id to the DbRow constructor without passing also a TDBMService.'); |
|
106 | + } |
|
107 | + } else { |
|
108 | + $this->tdbmService = $tdbmService; |
|
109 | + |
|
110 | + if (!empty($primaryKeys)) { |
|
111 | + $this->_setPrimaryKeys($primaryKeys); |
|
112 | + if (!empty($dbRow)) { |
|
113 | + $this->dbRow = $dbRow; |
|
114 | + $this->status = TDBMObjectStateEnum::STATE_LOADED; |
|
115 | + } else { |
|
116 | + $this->status = TDBMObjectStateEnum::STATE_NOT_LOADED; |
|
117 | + } |
|
118 | + $tdbmService->_addToCache($this); |
|
119 | + } else { |
|
120 | + $this->status = TDBMObjectStateEnum::STATE_NEW; |
|
121 | + $this->tdbmService->_addToToSaveObjectList($this); |
|
122 | + } |
|
123 | + } |
|
124 | + } |
|
125 | + |
|
126 | + public function _attach(TDBMService $tdbmService) |
|
127 | + { |
|
128 | + if ($this->status !== TDBMObjectStateEnum::STATE_DETACHED) { |
|
129 | + throw new TDBMInvalidOperationException('Cannot attach an object that is already attached to TDBM.'); |
|
130 | + } |
|
131 | + $this->tdbmService = $tdbmService; |
|
132 | + $this->status = TDBMObjectStateEnum::STATE_NEW; |
|
133 | + $this->tdbmService->_addToToSaveObjectList($this); |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * Sets the state of the TDBM Object |
|
138 | + * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
139 | + * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
140 | + * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
141 | + * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
142 | + * |
|
143 | + * @param string $state |
|
144 | + */ |
|
145 | + public function _setStatus($state) |
|
146 | + { |
|
147 | + $this->status = $state; |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * This is an internal method. You should not call this method yourself. The TDBM library will do it for you. |
|
152 | + * If the object is in state 'not loaded', this method performs a query in database to load the object. |
|
153 | + * |
|
154 | + * A TDBMException is thrown is no object can be retrieved (for instance, if the primary key specified |
|
155 | + * cannot be found). |
|
156 | + */ |
|
157 | + public function _dbLoadIfNotLoaded() |
|
158 | + { |
|
159 | + if ($this->status == TDBMObjectStateEnum::STATE_NOT_LOADED) { |
|
160 | + $connection = $this->tdbmService->getConnection(); |
|
161 | + |
|
162 | + /// buildFilterFromFilterBag($filter_bag) |
|
163 | + list($sql_where, $parameters) = $this->tdbmService->buildFilterFromFilterBag($this->primaryKeys); |
|
164 | + |
|
165 | + $sql = 'SELECT * FROM '.$connection->quoteIdentifier($this->dbTableName).' WHERE '.$sql_where; |
|
166 | + $result = $connection->executeQuery($sql, $parameters); |
|
167 | + |
|
168 | + if ($result->rowCount() === 0) { |
|
169 | + throw new TDBMException("Could not retrieve object from table \"$this->dbTableName\" using filter \"\"."); |
|
170 | + } |
|
171 | + |
|
172 | + $row = $result->fetch(\PDO::FETCH_ASSOC); |
|
173 | + |
|
174 | + $this->dbRow = []; |
|
175 | + $types = $this->tdbmService->_getColumnTypesForTable($this->dbTableName); |
|
176 | + |
|
177 | + foreach ($row as $key => $value) { |
|
178 | + $this->dbRow[$key] = $types[$key]->convertToPHPValue($value, $connection->getDatabasePlatform()); |
|
179 | + } |
|
180 | + |
|
181 | + $result->closeCursor(); |
|
182 | + |
|
183 | + $this->status = TDBMObjectStateEnum::STATE_LOADED; |
|
184 | + } |
|
185 | + } |
|
186 | + |
|
187 | + public function get($var) |
|
188 | + { |
|
189 | + $this->_dbLoadIfNotLoaded(); |
|
190 | + |
|
191 | + // Let's first check if the key exist. |
|
192 | + if (!isset($this->dbRow[$var])) { |
|
193 | + /* |
|
194 | 194 | // Unable to find column.... this is an error if the object has been retrieved from database. |
195 | 195 | // If it's a new object, well, that may not be an error after all! |
196 | 196 | // Let's check if the column does exist in the table |
@@ -210,39 +210,39 @@ discard block |
||
210 | 210 | $str = "Could not find column \"$var\" in table \"$this->dbTableName\". Maybe you meant one of those columns: '".implode("', '",$result_array)."'"; |
211 | 211 | |
212 | 212 | throw new TDBMException($str);*/ |
213 | - return; |
|
214 | - } |
|
215 | - |
|
216 | - $value = $this->dbRow[$var]; |
|
217 | - if ($value instanceof \DateTime) { |
|
218 | - if (method_exists('DateTimeImmutable', 'createFromMutable')) { // PHP 5.6+ only |
|
219 | - return \DateTimeImmutable::createFromMutable($value); |
|
220 | - } else { |
|
221 | - return new \DateTimeImmutable($value->format('c')); |
|
222 | - } |
|
223 | - } |
|
224 | - |
|
225 | - return $this->dbRow[$var]; |
|
226 | - } |
|
227 | - |
|
228 | - /** |
|
229 | - * Returns true if a column is set, false otherwise. |
|
230 | - * |
|
231 | - * @param string $var |
|
232 | - * |
|
233 | - * @return bool |
|
234 | - */ |
|
235 | - /*public function has($var) { |
|
213 | + return; |
|
214 | + } |
|
215 | + |
|
216 | + $value = $this->dbRow[$var]; |
|
217 | + if ($value instanceof \DateTime) { |
|
218 | + if (method_exists('DateTimeImmutable', 'createFromMutable')) { // PHP 5.6+ only |
|
219 | + return \DateTimeImmutable::createFromMutable($value); |
|
220 | + } else { |
|
221 | + return new \DateTimeImmutable($value->format('c')); |
|
222 | + } |
|
223 | + } |
|
224 | + |
|
225 | + return $this->dbRow[$var]; |
|
226 | + } |
|
227 | + |
|
228 | + /** |
|
229 | + * Returns true if a column is set, false otherwise. |
|
230 | + * |
|
231 | + * @param string $var |
|
232 | + * |
|
233 | + * @return bool |
|
234 | + */ |
|
235 | + /*public function has($var) { |
|
236 | 236 | $this->_dbLoadIfNotLoaded(); |
237 | 237 | |
238 | 238 | return isset($this->dbRow[$var]); |
239 | 239 | }*/ |
240 | 240 | |
241 | - public function set($var, $value) |
|
242 | - { |
|
243 | - $this->_dbLoadIfNotLoaded(); |
|
241 | + public function set($var, $value) |
|
242 | + { |
|
243 | + $this->_dbLoadIfNotLoaded(); |
|
244 | 244 | |
245 | - /* |
|
245 | + /* |
|
246 | 246 | // Ok, let's start by checking the column type |
247 | 247 | $type = $this->db_connection->getColumnType($this->dbTableName, $var); |
248 | 248 | |
@@ -252,194 +252,194 @@ discard block |
||
252 | 252 | } |
253 | 253 | */ |
254 | 254 | |
255 | - /*if ($var == $this->getPrimaryKey() && isset($this->dbRow[$var])) |
|
255 | + /*if ($var == $this->getPrimaryKey() && isset($this->dbRow[$var])) |
|
256 | 256 | throw new TDBMException("Error! Changing primary key value is forbidden.");*/ |
257 | - $this->dbRow[$var] = $value; |
|
258 | - if ($this->tdbmService !== null && $this->status === TDBMObjectStateEnum::STATE_LOADED) { |
|
259 | - $this->status = TDBMObjectStateEnum::STATE_DIRTY; |
|
260 | - $this->tdbmService->_addToToSaveObjectList($this); |
|
261 | - } |
|
262 | - } |
|
263 | - |
|
264 | - /** |
|
265 | - * @param string $foreignKeyName |
|
266 | - * @param AbstractTDBMObject $bean |
|
267 | - */ |
|
268 | - public function setRef($foreignKeyName, AbstractTDBMObject $bean = null) |
|
269 | - { |
|
270 | - $this->references[$foreignKeyName] = $bean; |
|
271 | - |
|
272 | - if ($this->tdbmService !== null && $this->status === TDBMObjectStateEnum::STATE_LOADED) { |
|
273 | - $this->status = TDBMObjectStateEnum::STATE_DIRTY; |
|
274 | - $this->tdbmService->_addToToSaveObjectList($this); |
|
275 | - } |
|
276 | - } |
|
277 | - |
|
278 | - /** |
|
279 | - * @param string $foreignKeyName A unique name for this reference |
|
280 | - * |
|
281 | - * @return AbstractTDBMObject|null |
|
282 | - */ |
|
283 | - public function getRef($foreignKeyName) |
|
284 | - { |
|
285 | - if (array_key_exists($foreignKeyName, $this->references)) { |
|
286 | - return $this->references[$foreignKeyName]; |
|
287 | - } elseif ($this->status === TDBMObjectStateEnum::STATE_NEW) { |
|
288 | - // If the object is new and has no property, then it has to be empty. |
|
289 | - return; |
|
290 | - } else { |
|
291 | - $this->_dbLoadIfNotLoaded(); |
|
292 | - |
|
293 | - // Let's match the name of the columns to the primary key values |
|
294 | - $fk = $this->tdbmService->_getForeignKeyByName($this->dbTableName, $foreignKeyName); |
|
295 | - |
|
296 | - $values = []; |
|
297 | - foreach ($fk->getLocalColumns() as $column) { |
|
298 | - $val = $this->dbRow[$column]; |
|
299 | - if ($val === null) { |
|
300 | - return; |
|
301 | - } |
|
302 | - $values[] = $val; |
|
303 | - } |
|
304 | - |
|
305 | - $filter = array_combine($this->tdbmService->getPrimaryKeyColumns($fk->getForeignTableName()), $values); |
|
306 | - |
|
307 | - return $this->tdbmService->findObjectByPk($fk->getForeignTableName(), $filter, [], true); |
|
308 | - } |
|
309 | - } |
|
310 | - |
|
311 | - /** |
|
312 | - * Returns the name of the table this object comes from. |
|
313 | - * |
|
314 | - * @return string |
|
315 | - */ |
|
316 | - public function _getDbTableName() |
|
317 | - { |
|
318 | - return $this->dbTableName; |
|
319 | - } |
|
320 | - |
|
321 | - /** |
|
322 | - * Method used internally by TDBM. You should not use it directly. |
|
323 | - * This method returns the status of the TDBMObject. |
|
324 | - * This is one of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
325 | - * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
326 | - * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
327 | - * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
328 | - * |
|
329 | - * @return string |
|
330 | - */ |
|
331 | - public function _getStatus() |
|
332 | - { |
|
333 | - return $this->status; |
|
334 | - } |
|
335 | - |
|
336 | - /** |
|
337 | - * Override the native php clone function for TDBMObjects. |
|
338 | - */ |
|
339 | - public function __clone() |
|
340 | - { |
|
341 | - // Let's load the row (before we lose the ID!) |
|
342 | - $this->_dbLoadIfNotLoaded(); |
|
343 | - |
|
344 | - //Let's set the status to detached |
|
345 | - $this->status = TDBMObjectStateEnum::STATE_DETACHED; |
|
346 | - |
|
347 | - $this->primaryKeys = []; |
|
348 | - |
|
349 | - //Now unset the PK from the row |
|
350 | - if ($this->tdbmService) { |
|
351 | - $pk_array = $this->tdbmService->getPrimaryKeyColumns($this->dbTableName); |
|
352 | - foreach ($pk_array as $pk) { |
|
353 | - $this->dbRow[$pk] = null; |
|
354 | - } |
|
355 | - } |
|
356 | - } |
|
357 | - |
|
358 | - /** |
|
359 | - * Returns raw database row. |
|
360 | - * |
|
361 | - * @return array |
|
362 | - */ |
|
363 | - public function _getDbRow() |
|
364 | - { |
|
365 | - // Let's merge $dbRow and $references |
|
366 | - $dbRow = $this->dbRow; |
|
367 | - |
|
368 | - foreach ($this->references as $foreignKeyName => $reference) { |
|
369 | - // Let's match the name of the columns to the primary key values |
|
370 | - $fk = $this->tdbmService->_getForeignKeyByName($this->dbTableName, $foreignKeyName); |
|
371 | - $localColumns = $fk->getLocalColumns(); |
|
372 | - |
|
373 | - if ($reference !== null) { |
|
374 | - $refDbRows = $reference->_getDbRows(); |
|
375 | - $firstRefDbRow = reset($refDbRows); |
|
376 | - $pkValues = array_values($firstRefDbRow->_getPrimaryKeys()); |
|
377 | - for ($i = 0, $count = count($localColumns); $i < $count; ++$i) { |
|
378 | - $dbRow[$localColumns[$i]] = $pkValues[$i]; |
|
379 | - } |
|
380 | - } else { |
|
381 | - for ($i = 0, $count = count($localColumns); $i < $count; ++$i) { |
|
382 | - $dbRow[$localColumns[$i]] = null; |
|
383 | - } |
|
384 | - } |
|
385 | - } |
|
386 | - |
|
387 | - return $dbRow; |
|
388 | - } |
|
389 | - |
|
390 | - /** |
|
391 | - * Returns references array. |
|
392 | - * |
|
393 | - * @return AbstractTDBMObject[] |
|
394 | - */ |
|
395 | - public function _getReferences() |
|
396 | - { |
|
397 | - return $this->references; |
|
398 | - } |
|
399 | - |
|
400 | - /** |
|
401 | - * Returns the values of the primary key. |
|
402 | - * This is set when the object is in "loaded" state. |
|
403 | - * |
|
404 | - * @return array |
|
405 | - */ |
|
406 | - public function _getPrimaryKeys() |
|
407 | - { |
|
408 | - return $this->primaryKeys; |
|
409 | - } |
|
410 | - |
|
411 | - /** |
|
412 | - * Sets the values of the primary key. |
|
413 | - * This is set when the object is in "loaded" state. |
|
414 | - * |
|
415 | - * @param array $primaryKeys |
|
416 | - */ |
|
417 | - public function _setPrimaryKeys(array $primaryKeys) |
|
418 | - { |
|
419 | - $this->primaryKeys = $primaryKeys; |
|
420 | - foreach ($this->primaryKeys as $column => $value) { |
|
421 | - $this->dbRow[$column] = $value; |
|
422 | - } |
|
423 | - } |
|
424 | - |
|
425 | - /** |
|
426 | - * Returns the TDBMObject this bean is associated to. |
|
427 | - * |
|
428 | - * @return AbstractTDBMObject |
|
429 | - */ |
|
430 | - public function getTDBMObject() |
|
431 | - { |
|
432 | - return $this->object; |
|
433 | - } |
|
434 | - |
|
435 | - /** |
|
436 | - * Sets the TDBMObject this bean is associated to. |
|
437 | - * Only used when cloning. |
|
438 | - * |
|
439 | - * @param AbstractTDBMObject $object |
|
440 | - */ |
|
441 | - public function setTDBMObject(AbstractTDBMObject $object) |
|
442 | - { |
|
443 | - $this->object = $object; |
|
444 | - } |
|
257 | + $this->dbRow[$var] = $value; |
|
258 | + if ($this->tdbmService !== null && $this->status === TDBMObjectStateEnum::STATE_LOADED) { |
|
259 | + $this->status = TDBMObjectStateEnum::STATE_DIRTY; |
|
260 | + $this->tdbmService->_addToToSaveObjectList($this); |
|
261 | + } |
|
262 | + } |
|
263 | + |
|
264 | + /** |
|
265 | + * @param string $foreignKeyName |
|
266 | + * @param AbstractTDBMObject $bean |
|
267 | + */ |
|
268 | + public function setRef($foreignKeyName, AbstractTDBMObject $bean = null) |
|
269 | + { |
|
270 | + $this->references[$foreignKeyName] = $bean; |
|
271 | + |
|
272 | + if ($this->tdbmService !== null && $this->status === TDBMObjectStateEnum::STATE_LOADED) { |
|
273 | + $this->status = TDBMObjectStateEnum::STATE_DIRTY; |
|
274 | + $this->tdbmService->_addToToSaveObjectList($this); |
|
275 | + } |
|
276 | + } |
|
277 | + |
|
278 | + /** |
|
279 | + * @param string $foreignKeyName A unique name for this reference |
|
280 | + * |
|
281 | + * @return AbstractTDBMObject|null |
|
282 | + */ |
|
283 | + public function getRef($foreignKeyName) |
|
284 | + { |
|
285 | + if (array_key_exists($foreignKeyName, $this->references)) { |
|
286 | + return $this->references[$foreignKeyName]; |
|
287 | + } elseif ($this->status === TDBMObjectStateEnum::STATE_NEW) { |
|
288 | + // If the object is new and has no property, then it has to be empty. |
|
289 | + return; |
|
290 | + } else { |
|
291 | + $this->_dbLoadIfNotLoaded(); |
|
292 | + |
|
293 | + // Let's match the name of the columns to the primary key values |
|
294 | + $fk = $this->tdbmService->_getForeignKeyByName($this->dbTableName, $foreignKeyName); |
|
295 | + |
|
296 | + $values = []; |
|
297 | + foreach ($fk->getLocalColumns() as $column) { |
|
298 | + $val = $this->dbRow[$column]; |
|
299 | + if ($val === null) { |
|
300 | + return; |
|
301 | + } |
|
302 | + $values[] = $val; |
|
303 | + } |
|
304 | + |
|
305 | + $filter = array_combine($this->tdbmService->getPrimaryKeyColumns($fk->getForeignTableName()), $values); |
|
306 | + |
|
307 | + return $this->tdbmService->findObjectByPk($fk->getForeignTableName(), $filter, [], true); |
|
308 | + } |
|
309 | + } |
|
310 | + |
|
311 | + /** |
|
312 | + * Returns the name of the table this object comes from. |
|
313 | + * |
|
314 | + * @return string |
|
315 | + */ |
|
316 | + public function _getDbTableName() |
|
317 | + { |
|
318 | + return $this->dbTableName; |
|
319 | + } |
|
320 | + |
|
321 | + /** |
|
322 | + * Method used internally by TDBM. You should not use it directly. |
|
323 | + * This method returns the status of the TDBMObject. |
|
324 | + * This is one of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
325 | + * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
326 | + * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
327 | + * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
328 | + * |
|
329 | + * @return string |
|
330 | + */ |
|
331 | + public function _getStatus() |
|
332 | + { |
|
333 | + return $this->status; |
|
334 | + } |
|
335 | + |
|
336 | + /** |
|
337 | + * Override the native php clone function for TDBMObjects. |
|
338 | + */ |
|
339 | + public function __clone() |
|
340 | + { |
|
341 | + // Let's load the row (before we lose the ID!) |
|
342 | + $this->_dbLoadIfNotLoaded(); |
|
343 | + |
|
344 | + //Let's set the status to detached |
|
345 | + $this->status = TDBMObjectStateEnum::STATE_DETACHED; |
|
346 | + |
|
347 | + $this->primaryKeys = []; |
|
348 | + |
|
349 | + //Now unset the PK from the row |
|
350 | + if ($this->tdbmService) { |
|
351 | + $pk_array = $this->tdbmService->getPrimaryKeyColumns($this->dbTableName); |
|
352 | + foreach ($pk_array as $pk) { |
|
353 | + $this->dbRow[$pk] = null; |
|
354 | + } |
|
355 | + } |
|
356 | + } |
|
357 | + |
|
358 | + /** |
|
359 | + * Returns raw database row. |
|
360 | + * |
|
361 | + * @return array |
|
362 | + */ |
|
363 | + public function _getDbRow() |
|
364 | + { |
|
365 | + // Let's merge $dbRow and $references |
|
366 | + $dbRow = $this->dbRow; |
|
367 | + |
|
368 | + foreach ($this->references as $foreignKeyName => $reference) { |
|
369 | + // Let's match the name of the columns to the primary key values |
|
370 | + $fk = $this->tdbmService->_getForeignKeyByName($this->dbTableName, $foreignKeyName); |
|
371 | + $localColumns = $fk->getLocalColumns(); |
|
372 | + |
|
373 | + if ($reference !== null) { |
|
374 | + $refDbRows = $reference->_getDbRows(); |
|
375 | + $firstRefDbRow = reset($refDbRows); |
|
376 | + $pkValues = array_values($firstRefDbRow->_getPrimaryKeys()); |
|
377 | + for ($i = 0, $count = count($localColumns); $i < $count; ++$i) { |
|
378 | + $dbRow[$localColumns[$i]] = $pkValues[$i]; |
|
379 | + } |
|
380 | + } else { |
|
381 | + for ($i = 0, $count = count($localColumns); $i < $count; ++$i) { |
|
382 | + $dbRow[$localColumns[$i]] = null; |
|
383 | + } |
|
384 | + } |
|
385 | + } |
|
386 | + |
|
387 | + return $dbRow; |
|
388 | + } |
|
389 | + |
|
390 | + /** |
|
391 | + * Returns references array. |
|
392 | + * |
|
393 | + * @return AbstractTDBMObject[] |
|
394 | + */ |
|
395 | + public function _getReferences() |
|
396 | + { |
|
397 | + return $this->references; |
|
398 | + } |
|
399 | + |
|
400 | + /** |
|
401 | + * Returns the values of the primary key. |
|
402 | + * This is set when the object is in "loaded" state. |
|
403 | + * |
|
404 | + * @return array |
|
405 | + */ |
|
406 | + public function _getPrimaryKeys() |
|
407 | + { |
|
408 | + return $this->primaryKeys; |
|
409 | + } |
|
410 | + |
|
411 | + /** |
|
412 | + * Sets the values of the primary key. |
|
413 | + * This is set when the object is in "loaded" state. |
|
414 | + * |
|
415 | + * @param array $primaryKeys |
|
416 | + */ |
|
417 | + public function _setPrimaryKeys(array $primaryKeys) |
|
418 | + { |
|
419 | + $this->primaryKeys = $primaryKeys; |
|
420 | + foreach ($this->primaryKeys as $column => $value) { |
|
421 | + $this->dbRow[$column] = $value; |
|
422 | + } |
|
423 | + } |
|
424 | + |
|
425 | + /** |
|
426 | + * Returns the TDBMObject this bean is associated to. |
|
427 | + * |
|
428 | + * @return AbstractTDBMObject |
|
429 | + */ |
|
430 | + public function getTDBMObject() |
|
431 | + { |
|
432 | + return $this->object; |
|
433 | + } |
|
434 | + |
|
435 | + /** |
|
436 | + * Sets the TDBMObject this bean is associated to. |
|
437 | + * Only used when cloning. |
|
438 | + * |
|
439 | + * @param AbstractTDBMObject $object |
|
440 | + */ |
|
441 | + public function setTDBMObject(AbstractTDBMObject $object) |
|
442 | + { |
|
443 | + $this->object = $object; |
|
444 | + } |
|
445 | 445 | } |
@@ -374,11 +374,11 @@ |
||
374 | 374 | $refDbRows = $reference->_getDbRows(); |
375 | 375 | $firstRefDbRow = reset($refDbRows); |
376 | 376 | $pkValues = array_values($firstRefDbRow->_getPrimaryKeys()); |
377 | - for ($i = 0, $count = count($localColumns); $i < $count; ++$i) { |
|
377 | + for ($i = 0, $count = count($localColumns); $i<$count; ++$i) { |
|
378 | 378 | $dbRow[$localColumns[$i]] = $pkValues[$i]; |
379 | 379 | } |
380 | 380 | } else { |
381 | - for ($i = 0, $count = count($localColumns); $i < $count; ++$i) { |
|
381 | + for ($i = 0, $count = count($localColumns); $i<$count; ++$i) { |
|
382 | 382 | $dbRow[$localColumns[$i]] = null; |
383 | 383 | } |
384 | 384 | } |
@@ -9,91 +9,91 @@ |
||
9 | 9 | */ |
10 | 10 | class MapIterator implements Iterator, \JsonSerializable |
11 | 11 | { |
12 | - /** |
|
13 | - * @var Iterator |
|
14 | - */ |
|
15 | - protected $iterator; |
|
12 | + /** |
|
13 | + * @var Iterator |
|
14 | + */ |
|
15 | + protected $iterator; |
|
16 | 16 | |
17 | - /** |
|
18 | - * @var callable Modifies the current item in iterator |
|
19 | - */ |
|
20 | - protected $callable; |
|
17 | + /** |
|
18 | + * @var callable Modifies the current item in iterator |
|
19 | + */ |
|
20 | + protected $callable; |
|
21 | 21 | |
22 | - /** |
|
23 | - * @param $iterator Iterator|array |
|
24 | - * @param $callable callable This can have two parameters |
|
25 | - * |
|
26 | - * @throws TDBMException |
|
27 | - */ |
|
28 | - public function __construct($iterator, callable $callable) |
|
29 | - { |
|
30 | - if (is_array($iterator)) { |
|
31 | - $this->iterator = new \ArrayIterator($iterator); |
|
32 | - } elseif (!($iterator instanceof Iterator)) { |
|
33 | - throw new TDBMException('$iterator parameter must be an instance of Iterator'); |
|
34 | - } else { |
|
35 | - $this->iterator = $iterator; |
|
36 | - } |
|
22 | + /** |
|
23 | + * @param $iterator Iterator|array |
|
24 | + * @param $callable callable This can have two parameters |
|
25 | + * |
|
26 | + * @throws TDBMException |
|
27 | + */ |
|
28 | + public function __construct($iterator, callable $callable) |
|
29 | + { |
|
30 | + if (is_array($iterator)) { |
|
31 | + $this->iterator = new \ArrayIterator($iterator); |
|
32 | + } elseif (!($iterator instanceof Iterator)) { |
|
33 | + throw new TDBMException('$iterator parameter must be an instance of Iterator'); |
|
34 | + } else { |
|
35 | + $this->iterator = $iterator; |
|
36 | + } |
|
37 | 37 | |
38 | - if ($callable instanceof \Closure) { |
|
39 | - // make sure there's one argument |
|
40 | - $reflection = new \ReflectionObject($callable); |
|
41 | - if ($reflection->hasMethod('__invoke')) { |
|
42 | - $method = $reflection->getMethod('__invoke'); |
|
43 | - if ($method->getNumberOfParameters() !== 1) { |
|
44 | - throw new TDBMException('$callable must accept one and only one parameter.'); |
|
45 | - } |
|
46 | - } |
|
47 | - } |
|
38 | + if ($callable instanceof \Closure) { |
|
39 | + // make sure there's one argument |
|
40 | + $reflection = new \ReflectionObject($callable); |
|
41 | + if ($reflection->hasMethod('__invoke')) { |
|
42 | + $method = $reflection->getMethod('__invoke'); |
|
43 | + if ($method->getNumberOfParameters() !== 1) { |
|
44 | + throw new TDBMException('$callable must accept one and only one parameter.'); |
|
45 | + } |
|
46 | + } |
|
47 | + } |
|
48 | 48 | |
49 | - $this->callable = $callable; |
|
50 | - } |
|
49 | + $this->callable = $callable; |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * Alters the current item with $this->callable and returns a new item. |
|
54 | - * Be careful with your types as we can't do static type checking here! |
|
55 | - * |
|
56 | - * @return mixed |
|
57 | - */ |
|
58 | - public function current() |
|
59 | - { |
|
60 | - $callable = $this->callable; |
|
52 | + /** |
|
53 | + * Alters the current item with $this->callable and returns a new item. |
|
54 | + * Be careful with your types as we can't do static type checking here! |
|
55 | + * |
|
56 | + * @return mixed |
|
57 | + */ |
|
58 | + public function current() |
|
59 | + { |
|
60 | + $callable = $this->callable; |
|
61 | 61 | |
62 | - return $callable($this->iterator->current()); |
|
63 | - } |
|
62 | + return $callable($this->iterator->current()); |
|
63 | + } |
|
64 | 64 | |
65 | - public function next() |
|
66 | - { |
|
67 | - $this->iterator->next(); |
|
68 | - } |
|
65 | + public function next() |
|
66 | + { |
|
67 | + $this->iterator->next(); |
|
68 | + } |
|
69 | 69 | |
70 | - public function key() |
|
71 | - { |
|
72 | - return $this->iterator->key(); |
|
73 | - } |
|
70 | + public function key() |
|
71 | + { |
|
72 | + return $this->iterator->key(); |
|
73 | + } |
|
74 | 74 | |
75 | - public function valid() |
|
76 | - { |
|
77 | - return $this->iterator->valid(); |
|
78 | - } |
|
75 | + public function valid() |
|
76 | + { |
|
77 | + return $this->iterator->valid(); |
|
78 | + } |
|
79 | 79 | |
80 | - public function rewind() |
|
81 | - { |
|
82 | - $this->iterator->rewind(); |
|
83 | - } |
|
80 | + public function rewind() |
|
81 | + { |
|
82 | + $this->iterator->rewind(); |
|
83 | + } |
|
84 | 84 | |
85 | - /** |
|
86 | - * Casts the iterator to a PHP array. |
|
87 | - * |
|
88 | - * @return array |
|
89 | - */ |
|
90 | - public function toArray() |
|
91 | - { |
|
92 | - return iterator_to_array($this); |
|
93 | - } |
|
85 | + /** |
|
86 | + * Casts the iterator to a PHP array. |
|
87 | + * |
|
88 | + * @return array |
|
89 | + */ |
|
90 | + public function toArray() |
|
91 | + { |
|
92 | + return iterator_to_array($this); |
|
93 | + } |
|
94 | 94 | |
95 | - public function jsonSerialize() |
|
96 | - { |
|
97 | - return $this->toArray(); |
|
98 | - } |
|
95 | + public function jsonSerialize() |
|
96 | + { |
|
97 | + return $this->toArray(); |
|
98 | + } |
|
99 | 99 | } |