Completed
Push — 4.0 ( 1abf1e...984e8a )
by David
05:40
created
src/Mouf/Database/TDBM/StandardObjectStorage.php 1 patch
Indentation   +69 added lines, -69 removed lines patch added patch discarded remove patch
@@ -30,78 +30,78 @@
 block discarded – undo
30 30
  */
31 31
 class StandardObjectStorage
32 32
 {
33
-    /**
34
-     * An array of fetched object, accessible via table name and primary key.
35
-     * If the primary key is split on several columns, access is done by an array of columns, serialized.
36
-     *
37
-     * @var array<string, array<string, TDBMObject>>
38
-     */
39
-    private $objects = array();
33
+	/**
34
+	 * An array of fetched object, accessible via table name and primary key.
35
+	 * If the primary key is split on several columns, access is done by an array of columns, serialized.
36
+	 *
37
+	 * @var array<string, array<string, TDBMObject>>
38
+	 */
39
+	private $objects = array();
40 40
 
41
-    /**
42
-     * Sets an object in the storage.
43
-     *
44
-     * @param string     $tableName
45
-     * @param string     $id
46
-     * @param TDBMObject $dbRow
47
-     */
48
-    public function set($tableName, $id, DbRow $dbRow)
49
-    {
50
-        $this->objects[$tableName][$id] = $dbRow;
51
-    }
41
+	/**
42
+	 * Sets an object in the storage.
43
+	 *
44
+	 * @param string     $tableName
45
+	 * @param string     $id
46
+	 * @param TDBMObject $dbRow
47
+	 */
48
+	public function set($tableName, $id, DbRow $dbRow)
49
+	{
50
+		$this->objects[$tableName][$id] = $dbRow;
51
+	}
52 52
 
53
-    /**
54
-     * Checks if an object is in the storage.
55
-     *
56
-     * @param string $tableName
57
-     * @param string $id
58
-     *
59
-     * @return bool
60
-     */
61
-    public function has($tableName, $id)
62
-    {
63
-        return isset($this->objects[$tableName][$id]);
64
-    }
53
+	/**
54
+	 * Checks if an object is in the storage.
55
+	 *
56
+	 * @param string $tableName
57
+	 * @param string $id
58
+	 *
59
+	 * @return bool
60
+	 */
61
+	public function has($tableName, $id)
62
+	{
63
+		return isset($this->objects[$tableName][$id]);
64
+	}
65 65
 
66
-    /**
67
-     * Returns an object from the storage (or null if no object is set).
68
-     *
69
-     * @param string $tableName
70
-     * @param string $id
71
-     *
72
-     * @return DbRow
73
-     */
74
-    public function get($tableName, $id)
75
-    {
76
-        if (isset($this->objects[$tableName][$id])) {
77
-            return $this->objects[$tableName][$id];
78
-        } else {
79
-            return;
80
-        }
81
-    }
66
+	/**
67
+	 * Returns an object from the storage (or null if no object is set).
68
+	 *
69
+	 * @param string $tableName
70
+	 * @param string $id
71
+	 *
72
+	 * @return DbRow
73
+	 */
74
+	public function get($tableName, $id)
75
+	{
76
+		if (isset($this->objects[$tableName][$id])) {
77
+			return $this->objects[$tableName][$id];
78
+		} else {
79
+			return;
80
+		}
81
+	}
82 82
 
83
-    /**
84
-     * Removes an object from the storage.
85
-     *
86
-     * @param string $tableName
87
-     * @param string $id
88
-     */
89
-    public function remove($tableName, $id)
90
-    {
91
-        unset($this->objects[$tableName][$id]);
92
-    }
83
+	/**
84
+	 * Removes an object from the storage.
85
+	 *
86
+	 * @param string $tableName
87
+	 * @param string $id
88
+	 */
89
+	public function remove($tableName, $id)
90
+	{
91
+		unset($this->objects[$tableName][$id]);
92
+	}
93 93
 
94
-    /**
95
-     * Applies the callback to all objects.
96
-     *
97
-     * @param callable $callback
98
-     */
99
-    public function apply(callable $callback)
100
-    {
101
-        foreach ($this->objects as $tableName => $table) {
102
-            foreach ($table as $id => $obj) {
103
-                $callback($obj, $tableName, $id);
104
-            }
105
-        }
106
-    }
94
+	/**
95
+	 * Applies the callback to all objects.
96
+	 *
97
+	 * @param callable $callback
98
+	 */
99
+	public function apply(callable $callback)
100
+	{
101
+		foreach ($this->objects as $tableName => $table) {
102
+			foreach ($table as $id => $obj) {
103
+				$callback($obj, $tableName, $id);
104
+			}
105
+		}
106
+	}
107 107
 }
Please login to merge, or discard this patch.
src/Mouf/Database/TDBM/AbstractTDBMObject.php 2 patches
Indentation   +517 added lines, -517 removed lines patch added patch discarded remove patch
@@ -29,521 +29,521 @@
 block discarded – undo
29 29
  */
30 30
 abstract class AbstractTDBMObject
31 31
 {
32
-    /**
33
-     * The service this object is bound to.
34
-     *
35
-     * @var TDBMService
36
-     */
37
-    protected $tdbmService;
38
-
39
-    /**
40
-     * An array of DbRow, indexed by table name.
41
-     *
42
-     * @var DbRow[]
43
-     */
44
-    protected $dbRows = array();
45
-
46
-    /**
47
-     * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED.
48
-     * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject.
49
-     * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet.
50
-     * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory.
51
-     *
52
-     * @var string
53
-     */
54
-    private $status;
55
-
56
-    /**
57
-     * Array storing beans related via many to many relationships (pivot tables).
58
-     *
59
-     * @var \SplObjectStorage[] Key: pivot table name, value: SplObjectStorage
60
-     */
61
-    private $relationships = [];
62
-
63
-    /**
64
-     * @var bool[] Key: pivot table name, value: whether a query was performed to load the data.
65
-     */
66
-    private $loadedRelationships = [];
67
-
68
-    /**
69
-     * Used with $primaryKeys when we want to retrieve an existing object
70
-     * and $primaryKeys=[] if we want a new object.
71
-     *
72
-     * @param string      $tableName
73
-     * @param array       $primaryKeys
74
-     * @param TDBMService $tdbmService
75
-     *
76
-     * @throws TDBMException
77
-     * @throws TDBMInvalidOperationException
78
-     */
79
-    public function __construct($tableName = null, array $primaryKeys = array(), TDBMService $tdbmService = null)
80
-    {
81
-        // FIXME: lazy loading should be forbidden on tables with inheritance and dynamic type assignation...
82
-        if (!empty($tableName)) {
83
-            $this->dbRows[$tableName] = new DbRow($this, $tableName, $primaryKeys, $tdbmService);
84
-        }
85
-
86
-        if ($tdbmService === null) {
87
-            $this->_setStatus(TDBMObjectStateEnum::STATE_DETACHED);
88
-        } else {
89
-            $this->_attach($tdbmService);
90
-            if (!empty($primaryKeys)) {
91
-                $this->_setStatus(TDBMObjectStateEnum::STATE_NOT_LOADED);
92
-            } else {
93
-                $this->_setStatus(TDBMObjectStateEnum::STATE_NEW);
94
-            }
95
-        }
96
-    }
97
-
98
-    /**
99
-     * Alternative constructor called when data is fetched from database via a SELECT.
100
-     *
101
-     * @param array       $beanData    array<table, array<column, value>>
102
-     * @param TDBMService $tdbmService
103
-     */
104
-    public function _constructFromData(array $beanData, TDBMService $tdbmService)
105
-    {
106
-        $this->tdbmService = $tdbmService;
107
-
108
-        foreach ($beanData as $table => $columns) {
109
-            $this->dbRows[$table] = new DbRow($this, $table, $tdbmService->_getPrimaryKeysFromObjectData($table, $columns), $tdbmService, $columns);
110
-        }
111
-
112
-        $this->status = TDBMObjectStateEnum::STATE_LOADED;
113
-    }
114
-
115
-    /**
116
-     * Alternative constructor called when bean is lazily loaded.
117
-     *
118
-     * @param string      $tableName
119
-     * @param array       $primaryKeys
120
-     * @param TDBMService $tdbmService
121
-     */
122
-    public function _constructLazy($tableName, array $primaryKeys, TDBMService $tdbmService)
123
-    {
124
-        $this->tdbmService = $tdbmService;
125
-
126
-        $this->dbRows[$tableName] = new DbRow($this, $tableName, $primaryKeys, $tdbmService);
127
-
128
-        $this->status = TDBMObjectStateEnum::STATE_NOT_LOADED;
129
-    }
130
-
131
-    public function _attach(TDBMService $tdbmService)
132
-    {
133
-        if ($this->status !== TDBMObjectStateEnum::STATE_DETACHED) {
134
-            throw new TDBMInvalidOperationException('Cannot attach an object that is already attached to TDBM.');
135
-        }
136
-        $this->tdbmService = $tdbmService;
137
-
138
-        // If we attach this object, we must work to make sure the tables are in ascending order (from low level to top level)
139
-        $tableNames = array_keys($this->dbRows);
140
-        $tableNames = $this->tdbmService->_getLinkBetweenInheritedTables($tableNames);
141
-        $tableNames = array_reverse($tableNames);
142
-
143
-        $newDbRows = [];
144
-
145
-        foreach ($tableNames as $table) {
146
-            if (!isset($this->dbRows[$table])) {
147
-                $this->registerTable($table);
148
-            }
149
-            $newDbRows[$table] = $this->dbRows[$table];
150
-        }
151
-        $this->dbRows = $newDbRows;
152
-
153
-        $this->status = TDBMObjectStateEnum::STATE_NEW;
154
-        foreach ($this->dbRows as $dbRow) {
155
-            $dbRow->_attach($tdbmService);
156
-        }
157
-    }
158
-
159
-    /**
160
-     * Sets the state of the TDBM Object
161
-     * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED.
162
-     * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject.
163
-     * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet.
164
-     * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory.
165
-     *
166
-     * @param string $state
167
-     */
168
-    public function _setStatus($state)
169
-    {
170
-        $this->status = $state;
171
-
172
-        // TODO: we might ignore the loaded => dirty state here! dirty status comes from the db_row itself.
173
-        foreach ($this->dbRows as $dbRow) {
174
-            $dbRow->_setStatus($state);
175
-        }
176
-    }
177
-
178
-    public function get($var, $tableName = null)
179
-    {
180
-        if ($tableName === null) {
181
-            if (count($this->dbRows) > 1) {
182
-                throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.');
183
-            } elseif (count($this->dbRows) === 1) {
184
-                $tableName = array_keys($this->dbRows)[0];
185
-            }
186
-        }
187
-
188
-        if (!isset($this->dbRows[$tableName])) {
189
-            if (count($this->dbRows[$tableName] === 0)) {
190
-                throw new TDBMException('Object is not yet bound to any table.');
191
-            } else {
192
-                throw new TDBMException('Unknown table "'.$tableName.'"" in object.');
193
-            }
194
-        }
195
-
196
-        return $this->dbRows[$tableName]->get($var);
197
-    }
198
-
199
-    /**
200
-     * Returns true if a column is set, false otherwise.
201
-     *
202
-     * @param string $var
203
-     *
204
-     * @return bool
205
-     */
206
-    public function has($var, $tableName = null)
207
-    {
208
-        if ($tableName === null) {
209
-            if (count($this->dbRows) > 1) {
210
-                throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.');
211
-            } elseif (count($this->dbRows) === 1) {
212
-                $tableName = array_keys($this->dbRows)[0];
213
-            }
214
-        }
215
-
216
-        if (!isset($this->dbRows[$tableName])) {
217
-            if (count($this->dbRows[$tableName] === 0)) {
218
-                throw new TDBMException('Object is not yet bound to any table.');
219
-            } else {
220
-                throw new TDBMException('Unknown table "'.$tableName.'"" in object.');
221
-            }
222
-        }
223
-
224
-        return $this->dbRows[$tableName]->has($var);
225
-    }
226
-
227
-    public function set($var, $value, $tableName = null)
228
-    {
229
-        if ($tableName === null) {
230
-            if (count($this->dbRows) > 1) {
231
-                throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.');
232
-            } elseif (count($this->dbRows) === 1) {
233
-                $tableName = array_keys($this->dbRows)[0];
234
-            } else {
235
-                throw new TDBMException('Please specify a table for this object.');
236
-            }
237
-        }
238
-
239
-        if (!isset($this->dbRows[$tableName])) {
240
-            $this->registerTable($tableName);
241
-        }
242
-
243
-        $this->dbRows[$tableName]->set($var, $value);
244
-        if ($this->dbRows[$tableName]->_getStatus() === TDBMObjectStateEnum::STATE_DIRTY) {
245
-            $this->status = TDBMObjectStateEnum::STATE_DIRTY;
246
-        }
247
-    }
248
-
249
-    /**
250
-     * @param string             $foreignKeyName
251
-     * @param AbstractTDBMObject $bean
252
-     */
253
-    public function setRef($foreignKeyName, AbstractTDBMObject $bean, $tableName = null)
254
-    {
255
-        if ($tableName === null) {
256
-            if (count($this->dbRows) > 1) {
257
-                throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.');
258
-            } elseif (count($this->dbRows) === 1) {
259
-                $tableName = array_keys($this->dbRows)[0];
260
-            } else {
261
-                throw new TDBMException('Please specify a table for this object.');
262
-            }
263
-        }
264
-
265
-        if (!isset($this->dbRows[$tableName])) {
266
-            $this->registerTable($tableName);
267
-        }
268
-
269
-        $this->dbRows[$tableName]->setRef($foreignKeyName, $bean);
270
-        if ($this->dbRows[$tableName]->_getStatus() === TDBMObjectStateEnum::STATE_DIRTY) {
271
-            $this->status = TDBMObjectStateEnum::STATE_DIRTY;
272
-        }
273
-    }
274
-
275
-    /**
276
-     * @param string $foreignKeyName A unique name for this reference
277
-     *
278
-     * @return AbstractTDBMObject|null
279
-     */
280
-    public function getRef($foreignKeyName, $tableName = null)
281
-    {
282
-        if ($tableName === null) {
283
-            if (count($this->dbRows) > 1) {
284
-                throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.');
285
-            } elseif (count($this->dbRows) === 1) {
286
-                $tableName = array_keys($this->dbRows)[0];
287
-            }
288
-        }
289
-
290
-        if (!isset($this->dbRows[$tableName])) {
291
-            if (count($this->dbRows[$tableName] === 0)) {
292
-                throw new TDBMException('Object is not yet bound to any table.');
293
-            } else {
294
-                throw new TDBMException('Unknown table "'.$tableName.'"" in object.');
295
-            }
296
-        }
297
-
298
-        return $this->dbRows[$tableName]->getRef($foreignKeyName);
299
-    }
300
-
301
-    /**
302
-     * Adds a many to many relationship to this bean.
303
-     *
304
-     * @param string             $pivotTableName
305
-     * @param AbstractTDBMObject $remoteBean
306
-     */
307
-    protected function addRelationship($pivotTableName, AbstractTDBMObject $remoteBean)
308
-    {
309
-        $this->setRelationship($pivotTableName, $remoteBean, 'new');
310
-    }
311
-
312
-    /**
313
-     * Returns true if there is a relationship to this bean.
314
-     *
315
-     * @param string             $pivotTableName
316
-     * @param AbstractTDBMObject $remoteBean
317
-     *
318
-     * @return bool
319
-     */
320
-    protected function hasRelationship($pivotTableName, AbstractTDBMObject $remoteBean)
321
-    {
322
-        $storage = $this->retrieveRelationshipsStorage($pivotTableName);
323
-
324
-        if ($storage->contains($remoteBean)) {
325
-            if ($storage[$remoteBean]['status'] !== 'delete') {
326
-                return true;
327
-            }
328
-        }
329
-
330
-        return false;
331
-    }
332
-
333
-    /**
334
-     * Internal TDBM method. Removes a many to many relationship from this bean.
335
-     *
336
-     * @param string             $pivotTableName
337
-     * @param AbstractTDBMObject $remoteBean
338
-     */
339
-    public function _removeRelationship($pivotTableName, AbstractTDBMObject $remoteBean)
340
-    {
341
-        if (isset($this->relationships[$pivotTableName][$remoteBean]) && $this->relationships[$pivotTableName][$remoteBean]['status'] === 'new') {
342
-            unset($this->relationships[$pivotTableName][$remoteBean]);
343
-            unset($remoteBean->relationships[$pivotTableName][$this]);
344
-        } else {
345
-            $this->setRelationship($pivotTableName, $remoteBean, 'delete');
346
-        }
347
-    }
348
-
349
-    /**
350
-     * Returns the list of objects linked to this bean via $pivotTableName.
351
-     *
352
-     * @param $pivotTableName
353
-     *
354
-     * @return \SplObjectStorage
355
-     */
356
-    private function retrieveRelationshipsStorage($pivotTableName)
357
-    {
358
-        $storage = $this->getRelationshipStorage($pivotTableName);
359
-        if ($this->status === TDBMObjectStateEnum::STATE_DETACHED || $this->status === TDBMObjectStateEnum::STATE_NEW || isset($this->loadedRelationships[$pivotTableName]) && $this->loadedRelationships[$pivotTableName]) {
360
-            return $storage;
361
-        }
362
-
363
-        $beans = $this->tdbmService->_getRelatedBeans($pivotTableName, $this);
364
-        $this->loadedRelationships[$pivotTableName] = true;
365
-
366
-        foreach ($beans as $bean) {
367
-            if (isset($storage[$bean])) {
368
-                $oldStatus = $storage[$bean]['status'];
369
-                if ($oldStatus === 'delete') {
370
-                    // Keep deleted things deleted
371
-                    continue;
372
-                }
373
-            }
374
-            $this->setRelationship($pivotTableName, $bean, 'loaded');
375
-        }
376
-
377
-        return $storage;
378
-    }
379
-
380
-    /**
381
-     * Internal TDBM method. Returns the list of objects linked to this bean via $pivotTableName.
382
-     *
383
-     * @param $pivotTableName
384
-     *
385
-     * @return AbstractTDBMObject[]
386
-     */
387
-    public function _getRelationships($pivotTableName)
388
-    {
389
-        return $this->relationshipStorageToArray($this->retrieveRelationshipsStorage($pivotTableName));
390
-    }
391
-
392
-    private function relationshipStorageToArray(\SplObjectStorage $storage)
393
-    {
394
-        $beans = [];
395
-        foreach ($storage as $bean) {
396
-            $statusArr = $storage[$bean];
397
-            if ($statusArr['status'] !== 'delete') {
398
-                $beans[] = $bean;
399
-            }
400
-        }
401
-
402
-        return $beans;
403
-    }
404
-
405
-    /**
406
-     * Declares a relationship between.
407
-     *
408
-     * @param string             $pivotTableName
409
-     * @param AbstractTDBMObject $remoteBean
410
-     * @param string             $status
411
-     */
412
-    private function setRelationship($pivotTableName, AbstractTDBMObject $remoteBean, $status)
413
-    {
414
-        $storage = $this->getRelationshipStorage($pivotTableName);
415
-        $storage->attach($remoteBean, ['status' => $status, 'reverse' => false]);
416
-        if ($this->status === TDBMObjectStateEnum::STATE_LOADED) {
417
-            $this->_setStatus(TDBMObjectStateEnum::STATE_DIRTY);
418
-        }
419
-
420
-        $remoteStorage = $remoteBean->getRelationshipStorage($pivotTableName);
421
-        $remoteStorage->attach($this, ['status' => $status, 'reverse' => true]);
422
-    }
423
-
424
-    /**
425
-     * Returns the SplObjectStorage associated to this relationship (creates it if it does not exists).
426
-     *
427
-     * @param $pivotTableName
428
-     *
429
-     * @return \SplObjectStorage
430
-     */
431
-    private function getRelationshipStorage($pivotTableName)
432
-    {
433
-        if (isset($this->relationships[$pivotTableName])) {
434
-            $storage = $this->relationships[$pivotTableName];
435
-        } else {
436
-            $storage = new \SplObjectStorage();
437
-            $this->relationships[$pivotTableName] = $storage;
438
-        }
439
-
440
-        return $storage;
441
-    }
442
-
443
-    /**
444
-     * Reverts any changes made to the object and resumes it to its DB state.
445
-     * This can only be called on objects that come from database and that have not been deleted.
446
-     * Otherwise, this will throw an exception.
447
-     *
448
-     * @throws TDBMException
449
-     */
450
-    public function discardChanges()
451
-    {
452
-        if ($this->status === TDBMObjectStateEnum::STATE_NEW || $this->status === TDBMObjectStateEnum::STATE_DETACHED) {
453
-            throw new TDBMException("You cannot call discardChanges() on an object that has been created with the 'new' keyword and that has not yet been saved.");
454
-        }
455
-
456
-        if ($this->status === TDBMObjectStateEnum::STATE_DELETED) {
457
-            throw new TDBMException('You cannot call discardChanges() on an object that has been deleted.');
458
-        }
459
-
460
-        $this->_setStatus(TDBMObjectStateEnum::STATE_NOT_LOADED);
461
-    }
462
-
463
-    /**
464
-     * Method used internally by TDBM. You should not use it directly.
465
-     * This method returns the status of the TDBMObject.
466
-     * This is one of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED.
467
-     * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject.
468
-     * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet.
469
-     * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory.
470
-     *
471
-     * @return string
472
-     */
473
-    public function _getStatus()
474
-    {
475
-        return $this->status;
476
-    }
477
-
478
-    /**
479
-     * Override the native php clone function for TDBMObjects.
480
-     */
481
-    public function __clone()
482
-    {
483
-        // Let's clone the many to many relationships
484
-        if ($this->status === TDBMObjectStateEnum::STATE_DETACHED) {
485
-            $pivotTableList = array_keys($this->relationships);
486
-        } else {
487
-            $pivotTableList = $this->tdbmService->_getPivotTablesLinkedToBean($this);
488
-        }
489
-
490
-        foreach ($pivotTableList as $pivotTable) {
491
-            $storage = $this->retrieveRelationshipsStorage($pivotTable);
492
-
493
-            // Let's duplicate the reverse side of the relationship
494
-            foreach ($storage as $remoteBean) {
495
-                $metadata = $storage[$remoteBean];
496
-
497
-                $remoteStorage = $remoteBean->getRelationshipStorage($pivotTable);
498
-                $remoteStorage->attach($this, ['status' => $metadata['status'], 'reverse' => !$metadata['reverse']]);
499
-            }
500
-        }
501
-
502
-        // Let's clone each row
503
-        foreach ($this->dbRows as $key => $dbRow) {
504
-            $this->dbRows[$key] = clone $dbRow;
505
-        }
506
-
507
-        // Let's set the status to new (to enter the save function)
508
-        $this->status = TDBMObjectStateEnum::STATE_DETACHED;
509
-    }
510
-
511
-    /**
512
-     * Returns raw database rows.
513
-     *
514
-     * @return DbRow[] Key: table name, Value: DbRow object
515
-     */
516
-    public function _getDbRows()
517
-    {
518
-        return $this->dbRows;
519
-    }
520
-
521
-    private function registerTable($tableName)
522
-    {
523
-        $dbRow = new DbRow($this, $tableName);
524
-
525
-        if (in_array($this->status, [TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DIRTY])) {
526
-            // Let's get the primary key for the new table
527
-            $anotherDbRow = array_values($this->dbRows)[0];
528
-            /* @var $anotherDbRow DbRow */
529
-            $indexedPrimaryKeys = array_values($anotherDbRow->_getPrimaryKeys());
530
-            $primaryKeys = $this->tdbmService->_getPrimaryKeysFromIndexedPrimaryKeys($tableName, $indexedPrimaryKeys);
531
-            $dbRow->_setPrimaryKeys($primaryKeys);
532
-        }
533
-
534
-        $dbRow->_setStatus($this->status);
535
-
536
-        $this->dbRows[$tableName] = $dbRow;
537
-        // TODO: look at status (if not new)=> get primary key from tdbmservice
538
-    }
539
-
540
-    /**
541
-     * Internal function: return the list of relationships.
542
-     *
543
-     * @return \SplObjectStorage[]
544
-     */
545
-    public function _getCachedRelationships()
546
-    {
547
-        return $this->relationships;
548
-    }
32
+	/**
33
+	 * The service this object is bound to.
34
+	 *
35
+	 * @var TDBMService
36
+	 */
37
+	protected $tdbmService;
38
+
39
+	/**
40
+	 * An array of DbRow, indexed by table name.
41
+	 *
42
+	 * @var DbRow[]
43
+	 */
44
+	protected $dbRows = array();
45
+
46
+	/**
47
+	 * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED.
48
+	 * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject.
49
+	 * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet.
50
+	 * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory.
51
+	 *
52
+	 * @var string
53
+	 */
54
+	private $status;
55
+
56
+	/**
57
+	 * Array storing beans related via many to many relationships (pivot tables).
58
+	 *
59
+	 * @var \SplObjectStorage[] Key: pivot table name, value: SplObjectStorage
60
+	 */
61
+	private $relationships = [];
62
+
63
+	/**
64
+	 * @var bool[] Key: pivot table name, value: whether a query was performed to load the data.
65
+	 */
66
+	private $loadedRelationships = [];
67
+
68
+	/**
69
+	 * Used with $primaryKeys when we want to retrieve an existing object
70
+	 * and $primaryKeys=[] if we want a new object.
71
+	 *
72
+	 * @param string      $tableName
73
+	 * @param array       $primaryKeys
74
+	 * @param TDBMService $tdbmService
75
+	 *
76
+	 * @throws TDBMException
77
+	 * @throws TDBMInvalidOperationException
78
+	 */
79
+	public function __construct($tableName = null, array $primaryKeys = array(), TDBMService $tdbmService = null)
80
+	{
81
+		// FIXME: lazy loading should be forbidden on tables with inheritance and dynamic type assignation...
82
+		if (!empty($tableName)) {
83
+			$this->dbRows[$tableName] = new DbRow($this, $tableName, $primaryKeys, $tdbmService);
84
+		}
85
+
86
+		if ($tdbmService === null) {
87
+			$this->_setStatus(TDBMObjectStateEnum::STATE_DETACHED);
88
+		} else {
89
+			$this->_attach($tdbmService);
90
+			if (!empty($primaryKeys)) {
91
+				$this->_setStatus(TDBMObjectStateEnum::STATE_NOT_LOADED);
92
+			} else {
93
+				$this->_setStatus(TDBMObjectStateEnum::STATE_NEW);
94
+			}
95
+		}
96
+	}
97
+
98
+	/**
99
+	 * Alternative constructor called when data is fetched from database via a SELECT.
100
+	 *
101
+	 * @param array       $beanData    array<table, array<column, value>>
102
+	 * @param TDBMService $tdbmService
103
+	 */
104
+	public function _constructFromData(array $beanData, TDBMService $tdbmService)
105
+	{
106
+		$this->tdbmService = $tdbmService;
107
+
108
+		foreach ($beanData as $table => $columns) {
109
+			$this->dbRows[$table] = new DbRow($this, $table, $tdbmService->_getPrimaryKeysFromObjectData($table, $columns), $tdbmService, $columns);
110
+		}
111
+
112
+		$this->status = TDBMObjectStateEnum::STATE_LOADED;
113
+	}
114
+
115
+	/**
116
+	 * Alternative constructor called when bean is lazily loaded.
117
+	 *
118
+	 * @param string      $tableName
119
+	 * @param array       $primaryKeys
120
+	 * @param TDBMService $tdbmService
121
+	 */
122
+	public function _constructLazy($tableName, array $primaryKeys, TDBMService $tdbmService)
123
+	{
124
+		$this->tdbmService = $tdbmService;
125
+
126
+		$this->dbRows[$tableName] = new DbRow($this, $tableName, $primaryKeys, $tdbmService);
127
+
128
+		$this->status = TDBMObjectStateEnum::STATE_NOT_LOADED;
129
+	}
130
+
131
+	public function _attach(TDBMService $tdbmService)
132
+	{
133
+		if ($this->status !== TDBMObjectStateEnum::STATE_DETACHED) {
134
+			throw new TDBMInvalidOperationException('Cannot attach an object that is already attached to TDBM.');
135
+		}
136
+		$this->tdbmService = $tdbmService;
137
+
138
+		// If we attach this object, we must work to make sure the tables are in ascending order (from low level to top level)
139
+		$tableNames = array_keys($this->dbRows);
140
+		$tableNames = $this->tdbmService->_getLinkBetweenInheritedTables($tableNames);
141
+		$tableNames = array_reverse($tableNames);
142
+
143
+		$newDbRows = [];
144
+
145
+		foreach ($tableNames as $table) {
146
+			if (!isset($this->dbRows[$table])) {
147
+				$this->registerTable($table);
148
+			}
149
+			$newDbRows[$table] = $this->dbRows[$table];
150
+		}
151
+		$this->dbRows = $newDbRows;
152
+
153
+		$this->status = TDBMObjectStateEnum::STATE_NEW;
154
+		foreach ($this->dbRows as $dbRow) {
155
+			$dbRow->_attach($tdbmService);
156
+		}
157
+	}
158
+
159
+	/**
160
+	 * Sets the state of the TDBM Object
161
+	 * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED.
162
+	 * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject.
163
+	 * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet.
164
+	 * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory.
165
+	 *
166
+	 * @param string $state
167
+	 */
168
+	public function _setStatus($state)
169
+	{
170
+		$this->status = $state;
171
+
172
+		// TODO: we might ignore the loaded => dirty state here! dirty status comes from the db_row itself.
173
+		foreach ($this->dbRows as $dbRow) {
174
+			$dbRow->_setStatus($state);
175
+		}
176
+	}
177
+
178
+	public function get($var, $tableName = null)
179
+	{
180
+		if ($tableName === null) {
181
+			if (count($this->dbRows) > 1) {
182
+				throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.');
183
+			} elseif (count($this->dbRows) === 1) {
184
+				$tableName = array_keys($this->dbRows)[0];
185
+			}
186
+		}
187
+
188
+		if (!isset($this->dbRows[$tableName])) {
189
+			if (count($this->dbRows[$tableName] === 0)) {
190
+				throw new TDBMException('Object is not yet bound to any table.');
191
+			} else {
192
+				throw new TDBMException('Unknown table "'.$tableName.'"" in object.');
193
+			}
194
+		}
195
+
196
+		return $this->dbRows[$tableName]->get($var);
197
+	}
198
+
199
+	/**
200
+	 * Returns true if a column is set, false otherwise.
201
+	 *
202
+	 * @param string $var
203
+	 *
204
+	 * @return bool
205
+	 */
206
+	public function has($var, $tableName = null)
207
+	{
208
+		if ($tableName === null) {
209
+			if (count($this->dbRows) > 1) {
210
+				throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.');
211
+			} elseif (count($this->dbRows) === 1) {
212
+				$tableName = array_keys($this->dbRows)[0];
213
+			}
214
+		}
215
+
216
+		if (!isset($this->dbRows[$tableName])) {
217
+			if (count($this->dbRows[$tableName] === 0)) {
218
+				throw new TDBMException('Object is not yet bound to any table.');
219
+			} else {
220
+				throw new TDBMException('Unknown table "'.$tableName.'"" in object.');
221
+			}
222
+		}
223
+
224
+		return $this->dbRows[$tableName]->has($var);
225
+	}
226
+
227
+	public function set($var, $value, $tableName = null)
228
+	{
229
+		if ($tableName === null) {
230
+			if (count($this->dbRows) > 1) {
231
+				throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.');
232
+			} elseif (count($this->dbRows) === 1) {
233
+				$tableName = array_keys($this->dbRows)[0];
234
+			} else {
235
+				throw new TDBMException('Please specify a table for this object.');
236
+			}
237
+		}
238
+
239
+		if (!isset($this->dbRows[$tableName])) {
240
+			$this->registerTable($tableName);
241
+		}
242
+
243
+		$this->dbRows[$tableName]->set($var, $value);
244
+		if ($this->dbRows[$tableName]->_getStatus() === TDBMObjectStateEnum::STATE_DIRTY) {
245
+			$this->status = TDBMObjectStateEnum::STATE_DIRTY;
246
+		}
247
+	}
248
+
249
+	/**
250
+	 * @param string             $foreignKeyName
251
+	 * @param AbstractTDBMObject $bean
252
+	 */
253
+	public function setRef($foreignKeyName, AbstractTDBMObject $bean, $tableName = null)
254
+	{
255
+		if ($tableName === null) {
256
+			if (count($this->dbRows) > 1) {
257
+				throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.');
258
+			} elseif (count($this->dbRows) === 1) {
259
+				$tableName = array_keys($this->dbRows)[0];
260
+			} else {
261
+				throw new TDBMException('Please specify a table for this object.');
262
+			}
263
+		}
264
+
265
+		if (!isset($this->dbRows[$tableName])) {
266
+			$this->registerTable($tableName);
267
+		}
268
+
269
+		$this->dbRows[$tableName]->setRef($foreignKeyName, $bean);
270
+		if ($this->dbRows[$tableName]->_getStatus() === TDBMObjectStateEnum::STATE_DIRTY) {
271
+			$this->status = TDBMObjectStateEnum::STATE_DIRTY;
272
+		}
273
+	}
274
+
275
+	/**
276
+	 * @param string $foreignKeyName A unique name for this reference
277
+	 *
278
+	 * @return AbstractTDBMObject|null
279
+	 */
280
+	public function getRef($foreignKeyName, $tableName = null)
281
+	{
282
+		if ($tableName === null) {
283
+			if (count($this->dbRows) > 1) {
284
+				throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.');
285
+			} elseif (count($this->dbRows) === 1) {
286
+				$tableName = array_keys($this->dbRows)[0];
287
+			}
288
+		}
289
+
290
+		if (!isset($this->dbRows[$tableName])) {
291
+			if (count($this->dbRows[$tableName] === 0)) {
292
+				throw new TDBMException('Object is not yet bound to any table.');
293
+			} else {
294
+				throw new TDBMException('Unknown table "'.$tableName.'"" in object.');
295
+			}
296
+		}
297
+
298
+		return $this->dbRows[$tableName]->getRef($foreignKeyName);
299
+	}
300
+
301
+	/**
302
+	 * Adds a many to many relationship to this bean.
303
+	 *
304
+	 * @param string             $pivotTableName
305
+	 * @param AbstractTDBMObject $remoteBean
306
+	 */
307
+	protected function addRelationship($pivotTableName, AbstractTDBMObject $remoteBean)
308
+	{
309
+		$this->setRelationship($pivotTableName, $remoteBean, 'new');
310
+	}
311
+
312
+	/**
313
+	 * Returns true if there is a relationship to this bean.
314
+	 *
315
+	 * @param string             $pivotTableName
316
+	 * @param AbstractTDBMObject $remoteBean
317
+	 *
318
+	 * @return bool
319
+	 */
320
+	protected function hasRelationship($pivotTableName, AbstractTDBMObject $remoteBean)
321
+	{
322
+		$storage = $this->retrieveRelationshipsStorage($pivotTableName);
323
+
324
+		if ($storage->contains($remoteBean)) {
325
+			if ($storage[$remoteBean]['status'] !== 'delete') {
326
+				return true;
327
+			}
328
+		}
329
+
330
+		return false;
331
+	}
332
+
333
+	/**
334
+	 * Internal TDBM method. Removes a many to many relationship from this bean.
335
+	 *
336
+	 * @param string             $pivotTableName
337
+	 * @param AbstractTDBMObject $remoteBean
338
+	 */
339
+	public function _removeRelationship($pivotTableName, AbstractTDBMObject $remoteBean)
340
+	{
341
+		if (isset($this->relationships[$pivotTableName][$remoteBean]) && $this->relationships[$pivotTableName][$remoteBean]['status'] === 'new') {
342
+			unset($this->relationships[$pivotTableName][$remoteBean]);
343
+			unset($remoteBean->relationships[$pivotTableName][$this]);
344
+		} else {
345
+			$this->setRelationship($pivotTableName, $remoteBean, 'delete');
346
+		}
347
+	}
348
+
349
+	/**
350
+	 * Returns the list of objects linked to this bean via $pivotTableName.
351
+	 *
352
+	 * @param $pivotTableName
353
+	 *
354
+	 * @return \SplObjectStorage
355
+	 */
356
+	private function retrieveRelationshipsStorage($pivotTableName)
357
+	{
358
+		$storage = $this->getRelationshipStorage($pivotTableName);
359
+		if ($this->status === TDBMObjectStateEnum::STATE_DETACHED || $this->status === TDBMObjectStateEnum::STATE_NEW || isset($this->loadedRelationships[$pivotTableName]) && $this->loadedRelationships[$pivotTableName]) {
360
+			return $storage;
361
+		}
362
+
363
+		$beans = $this->tdbmService->_getRelatedBeans($pivotTableName, $this);
364
+		$this->loadedRelationships[$pivotTableName] = true;
365
+
366
+		foreach ($beans as $bean) {
367
+			if (isset($storage[$bean])) {
368
+				$oldStatus = $storage[$bean]['status'];
369
+				if ($oldStatus === 'delete') {
370
+					// Keep deleted things deleted
371
+					continue;
372
+				}
373
+			}
374
+			$this->setRelationship($pivotTableName, $bean, 'loaded');
375
+		}
376
+
377
+		return $storage;
378
+	}
379
+
380
+	/**
381
+	 * Internal TDBM method. Returns the list of objects linked to this bean via $pivotTableName.
382
+	 *
383
+	 * @param $pivotTableName
384
+	 *
385
+	 * @return AbstractTDBMObject[]
386
+	 */
387
+	public function _getRelationships($pivotTableName)
388
+	{
389
+		return $this->relationshipStorageToArray($this->retrieveRelationshipsStorage($pivotTableName));
390
+	}
391
+
392
+	private function relationshipStorageToArray(\SplObjectStorage $storage)
393
+	{
394
+		$beans = [];
395
+		foreach ($storage as $bean) {
396
+			$statusArr = $storage[$bean];
397
+			if ($statusArr['status'] !== 'delete') {
398
+				$beans[] = $bean;
399
+			}
400
+		}
401
+
402
+		return $beans;
403
+	}
404
+
405
+	/**
406
+	 * Declares a relationship between.
407
+	 *
408
+	 * @param string             $pivotTableName
409
+	 * @param AbstractTDBMObject $remoteBean
410
+	 * @param string             $status
411
+	 */
412
+	private function setRelationship($pivotTableName, AbstractTDBMObject $remoteBean, $status)
413
+	{
414
+		$storage = $this->getRelationshipStorage($pivotTableName);
415
+		$storage->attach($remoteBean, ['status' => $status, 'reverse' => false]);
416
+		if ($this->status === TDBMObjectStateEnum::STATE_LOADED) {
417
+			$this->_setStatus(TDBMObjectStateEnum::STATE_DIRTY);
418
+		}
419
+
420
+		$remoteStorage = $remoteBean->getRelationshipStorage($pivotTableName);
421
+		$remoteStorage->attach($this, ['status' => $status, 'reverse' => true]);
422
+	}
423
+
424
+	/**
425
+	 * Returns the SplObjectStorage associated to this relationship (creates it if it does not exists).
426
+	 *
427
+	 * @param $pivotTableName
428
+	 *
429
+	 * @return \SplObjectStorage
430
+	 */
431
+	private function getRelationshipStorage($pivotTableName)
432
+	{
433
+		if (isset($this->relationships[$pivotTableName])) {
434
+			$storage = $this->relationships[$pivotTableName];
435
+		} else {
436
+			$storage = new \SplObjectStorage();
437
+			$this->relationships[$pivotTableName] = $storage;
438
+		}
439
+
440
+		return $storage;
441
+	}
442
+
443
+	/**
444
+	 * Reverts any changes made to the object and resumes it to its DB state.
445
+	 * This can only be called on objects that come from database and that have not been deleted.
446
+	 * Otherwise, this will throw an exception.
447
+	 *
448
+	 * @throws TDBMException
449
+	 */
450
+	public function discardChanges()
451
+	{
452
+		if ($this->status === TDBMObjectStateEnum::STATE_NEW || $this->status === TDBMObjectStateEnum::STATE_DETACHED) {
453
+			throw new TDBMException("You cannot call discardChanges() on an object that has been created with the 'new' keyword and that has not yet been saved.");
454
+		}
455
+
456
+		if ($this->status === TDBMObjectStateEnum::STATE_DELETED) {
457
+			throw new TDBMException('You cannot call discardChanges() on an object that has been deleted.');
458
+		}
459
+
460
+		$this->_setStatus(TDBMObjectStateEnum::STATE_NOT_LOADED);
461
+	}
462
+
463
+	/**
464
+	 * Method used internally by TDBM. You should not use it directly.
465
+	 * This method returns the status of the TDBMObject.
466
+	 * This is one of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED.
467
+	 * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject.
468
+	 * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet.
469
+	 * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory.
470
+	 *
471
+	 * @return string
472
+	 */
473
+	public function _getStatus()
474
+	{
475
+		return $this->status;
476
+	}
477
+
478
+	/**
479
+	 * Override the native php clone function for TDBMObjects.
480
+	 */
481
+	public function __clone()
482
+	{
483
+		// Let's clone the many to many relationships
484
+		if ($this->status === TDBMObjectStateEnum::STATE_DETACHED) {
485
+			$pivotTableList = array_keys($this->relationships);
486
+		} else {
487
+			$pivotTableList = $this->tdbmService->_getPivotTablesLinkedToBean($this);
488
+		}
489
+
490
+		foreach ($pivotTableList as $pivotTable) {
491
+			$storage = $this->retrieveRelationshipsStorage($pivotTable);
492
+
493
+			// Let's duplicate the reverse side of the relationship
494
+			foreach ($storage as $remoteBean) {
495
+				$metadata = $storage[$remoteBean];
496
+
497
+				$remoteStorage = $remoteBean->getRelationshipStorage($pivotTable);
498
+				$remoteStorage->attach($this, ['status' => $metadata['status'], 'reverse' => !$metadata['reverse']]);
499
+			}
500
+		}
501
+
502
+		// Let's clone each row
503
+		foreach ($this->dbRows as $key => $dbRow) {
504
+			$this->dbRows[$key] = clone $dbRow;
505
+		}
506
+
507
+		// Let's set the status to new (to enter the save function)
508
+		$this->status = TDBMObjectStateEnum::STATE_DETACHED;
509
+	}
510
+
511
+	/**
512
+	 * Returns raw database rows.
513
+	 *
514
+	 * @return DbRow[] Key: table name, Value: DbRow object
515
+	 */
516
+	public function _getDbRows()
517
+	{
518
+		return $this->dbRows;
519
+	}
520
+
521
+	private function registerTable($tableName)
522
+	{
523
+		$dbRow = new DbRow($this, $tableName);
524
+
525
+		if (in_array($this->status, [TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DIRTY])) {
526
+			// Let's get the primary key for the new table
527
+			$anotherDbRow = array_values($this->dbRows)[0];
528
+			/* @var $anotherDbRow DbRow */
529
+			$indexedPrimaryKeys = array_values($anotherDbRow->_getPrimaryKeys());
530
+			$primaryKeys = $this->tdbmService->_getPrimaryKeysFromIndexedPrimaryKeys($tableName, $indexedPrimaryKeys);
531
+			$dbRow->_setPrimaryKeys($primaryKeys);
532
+		}
533
+
534
+		$dbRow->_setStatus($this->status);
535
+
536
+		$this->dbRows[$tableName] = $dbRow;
537
+		// TODO: look at status (if not new)=> get primary key from tdbmservice
538
+	}
539
+
540
+	/**
541
+	 * Internal function: return the list of relationships.
542
+	 *
543
+	 * @return \SplObjectStorage[]
544
+	 */
545
+	public function _getCachedRelationships()
546
+	{
547
+		return $this->relationships;
548
+	}
549 549
 }
Please login to merge, or discard this patch.
Spacing   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -178,7 +178,7 @@  discard block
 block discarded – undo
178 178
     public function get($var, $tableName = null)
179 179
     {
180 180
         if ($tableName === null) {
181
-            if (count($this->dbRows) > 1) {
181
+            if (count($this->dbRows)>1) {
182 182
                 throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.');
183 183
             } elseif (count($this->dbRows) === 1) {
184 184
                 $tableName = array_keys($this->dbRows)[0];
@@ -206,7 +206,7 @@  discard block
 block discarded – undo
206 206
     public function has($var, $tableName = null)
207 207
     {
208 208
         if ($tableName === null) {
209
-            if (count($this->dbRows) > 1) {
209
+            if (count($this->dbRows)>1) {
210 210
                 throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.');
211 211
             } elseif (count($this->dbRows) === 1) {
212 212
                 $tableName = array_keys($this->dbRows)[0];
@@ -227,7 +227,7 @@  discard block
 block discarded – undo
227 227
     public function set($var, $value, $tableName = null)
228 228
     {
229 229
         if ($tableName === null) {
230
-            if (count($this->dbRows) > 1) {
230
+            if (count($this->dbRows)>1) {
231 231
                 throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.');
232 232
             } elseif (count($this->dbRows) === 1) {
233 233
                 $tableName = array_keys($this->dbRows)[0];
@@ -253,7 +253,7 @@  discard block
 block discarded – undo
253 253
     public function setRef($foreignKeyName, AbstractTDBMObject $bean, $tableName = null)
254 254
     {
255 255
         if ($tableName === null) {
256
-            if (count($this->dbRows) > 1) {
256
+            if (count($this->dbRows)>1) {
257 257
                 throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.');
258 258
             } elseif (count($this->dbRows) === 1) {
259 259
                 $tableName = array_keys($this->dbRows)[0];
@@ -280,7 +280,7 @@  discard block
 block discarded – undo
280 280
     public function getRef($foreignKeyName, $tableName = null)
281 281
     {
282 282
         if ($tableName === null) {
283
-            if (count($this->dbRows) > 1) {
283
+            if (count($this->dbRows)>1) {
284 284
                 throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.');
285 285
             } elseif (count($this->dbRows) === 1) {
286 286
                 $tableName = array_keys($this->dbRows)[0];
Please login to merge, or discard this patch.
src/Mouf/Database/TDBM/TDBMObjectArray.php 2 patches
Indentation   +65 added lines, -65 removed lines patch added patch discarded remove patch
@@ -26,77 +26,77 @@
 block discarded – undo
26 26
  */
27 27
 class TDBMObjectArray extends \ArrayObject implements \JsonSerializable
28 28
 {
29
-    public function __get($var)
30
-    {
31
-        $cnt = count($this);
32
-        if ($cnt == 1) {
33
-            return $this[0]->__get($var);
34
-        } elseif ($cnt > 1) {
35
-            throw new TDBMException('Array contains many objects! Use getarray_'.$var.' to retrieve an array of '.$var);
36
-        } else {
37
-            throw new TDBMException('Array contains no objects');
38
-        }
39
-    }
29
+	public function __get($var)
30
+	{
31
+		$cnt = count($this);
32
+		if ($cnt == 1) {
33
+			return $this[0]->__get($var);
34
+		} elseif ($cnt > 1) {
35
+			throw new TDBMException('Array contains many objects! Use getarray_'.$var.' to retrieve an array of '.$var);
36
+		} else {
37
+			throw new TDBMException('Array contains no objects');
38
+		}
39
+	}
40 40
 
41
-    public function __set($var, $value)
42
-    {
43
-        $cnt = count($this);
44
-        if ($cnt == 1) {
45
-            return $this[0]->__set($var, $value);
46
-        } elseif ($cnt > 1) {
47
-            throw new TDBMException('Array contains many objects! Use setarray_'.$var.' to set the array of '.$var);
48
-        } else {
49
-            throw new TDBMException('Array contains no objects');
50
-        }
51
-    }
41
+	public function __set($var, $value)
42
+	{
43
+		$cnt = count($this);
44
+		if ($cnt == 1) {
45
+			return $this[0]->__set($var, $value);
46
+		} elseif ($cnt > 1) {
47
+			throw new TDBMException('Array contains many objects! Use setarray_'.$var.' to set the array of '.$var);
48
+		} else {
49
+			throw new TDBMException('Array contains no objects');
50
+		}
51
+	}
52 52
 
53
-    /**
54
-     * getarray_column_name returns an array containing the values of the column of the given objects.
55
-     * setarray_column_name sets the value of the given column for all the objects.
56
-     *
57
-     * @param string $func_name
58
-     * @param $values
59
-     *
60
-     * @return array|void
61
-     *
62
-     * @throws TDBMException
63
-     */
64
-    public function __call($func_name, $values)
65
-    {
66
-        if (strpos($func_name, 'getarray_') === 0) {
67
-            $column = substr($func_name, 9);
53
+	/**
54
+	 * getarray_column_name returns an array containing the values of the column of the given objects.
55
+	 * setarray_column_name sets the value of the given column for all the objects.
56
+	 *
57
+	 * @param string $func_name
58
+	 * @param $values
59
+	 *
60
+	 * @return array|void
61
+	 *
62
+	 * @throws TDBMException
63
+	 */
64
+	public function __call($func_name, $values)
65
+	{
66
+		if (strpos($func_name, 'getarray_') === 0) {
67
+			$column = substr($func_name, 9);
68 68
 
69
-            return $this->getarray($column);
70
-        } elseif (strpos($func_name, 'setarray_') === 0) {
71
-            $column = substr($func_name, 9);
69
+			return $this->getarray($column);
70
+		} elseif (strpos($func_name, 'setarray_') === 0) {
71
+			$column = substr($func_name, 9);
72 72
 
73
-            return $this->setarray($column, $values[0]);
74
-        } elseif (count($this) == 1) {
75
-            $this[0]->__call($func_name, $values);
76
-        } else {
77
-            throw new TDBMException('Method '.$func_name.' not found');
78
-        }
79
-    }
73
+			return $this->setarray($column, $values[0]);
74
+		} elseif (count($this) == 1) {
75
+			$this[0]->__call($func_name, $values);
76
+		} else {
77
+			throw new TDBMException('Method '.$func_name.' not found');
78
+		}
79
+	}
80 80
 
81
-    private function getarray($column)
82
-    {
83
-        $arr = array();
84
-        foreach ($this as $object) {
85
-            $arr[] = $object->__get($column);
86
-        }
81
+	private function getarray($column)
82
+	{
83
+		$arr = array();
84
+		foreach ($this as $object) {
85
+			$arr[] = $object->__get($column);
86
+		}
87 87
 
88
-        return $arr;
89
-    }
88
+		return $arr;
89
+	}
90 90
 
91
-    private function setarray($column, $value)
92
-    {
93
-        foreach ($this as $object) {
94
-            $object->__set($column, $value);
95
-        }
96
-    }
91
+	private function setarray($column, $value)
92
+	{
93
+		foreach ($this as $object) {
94
+			$object->__set($column, $value);
95
+		}
96
+	}
97 97
 
98
-    public function jsonSerialize()
99
-    {
100
-        return (array) $this;
101
-    }
98
+	public function jsonSerialize()
99
+	{
100
+		return (array) $this;
101
+	}
102 102
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -31,7 +31,7 @@  discard block
 block discarded – undo
31 31
         $cnt = count($this);
32 32
         if ($cnt == 1) {
33 33
             return $this[0]->__get($var);
34
-        } elseif ($cnt > 1) {
34
+        } elseif ($cnt>1) {
35 35
             throw new TDBMException('Array contains many objects! Use getarray_'.$var.' to retrieve an array of '.$var);
36 36
         } else {
37 37
             throw new TDBMException('Array contains no objects');
@@ -43,7 +43,7 @@  discard block
 block discarded – undo
43 43
         $cnt = count($this);
44 44
         if ($cnt == 1) {
45 45
             return $this[0]->__set($var, $value);
46
-        } elseif ($cnt > 1) {
46
+        } elseif ($cnt>1) {
47 47
             throw new TDBMException('Array contains many objects! Use setarray_'.$var.' to set the array of '.$var);
48 48
         } else {
49 49
             throw new TDBMException('Array contains no objects');
Please login to merge, or discard this patch.
src/views/tdbmGenerate.php 1 patch
Indentation   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -8,7 +8,7 @@
 block discarded – undo
8 8
 <input type="hidden" id="selfedit" name="selfedit" value="<?php echo plainstring_to_htmlprotected($this->selfedit) ?>" />
9 9
 
10 10
 <?php if (!$this->autoloadDetected) {
11
-    ?>
11
+	?>
12 12
 <div class="alert">Warning! TDBM could not detect the autoload section of your composer.json file.
13 13
 Unless you are developing your own autoload system, you should configure <strong>composer.json</strong> to <a href="http://getcomposer.org/doc/01-basic-usage.md#autoloading" target="_blank">define a source directory and a root namespace using PSR-0</a>.</div>
14 14
 <?php 
Please login to merge, or discard this patch.