Completed
Push — 4.0 ( 7c26f2...05ee2e )
by David
03:13
created
src/Mouf/Database/TDBM/DbRow.php 1 patch
Indentation   +358 added lines, -358 removed lines patch added patch discarded remove patch
@@ -27,163 +27,163 @@  discard block
 block discarded – undo
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
-            $this->dbRow = $result->fetch(\PDO::FETCH_ASSOC);
173
-
174
-            $result->closeCursor();
175
-
176
-            $this->status = TDBMObjectStateEnum::STATE_LOADED;
177
-        }
178
-    }
179
-
180
-    public function get($var)
181
-    {
182
-        $this->_dbLoadIfNotLoaded();
183
-
184
-        // Let's first check if the key exist.
185
-        if (!isset($this->dbRow[$var])) {
186
-            /*
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
+			$this->dbRow = $result->fetch(\PDO::FETCH_ASSOC);
173
+
174
+			$result->closeCursor();
175
+
176
+			$this->status = TDBMObjectStateEnum::STATE_LOADED;
177
+		}
178
+	}
179
+
180
+	public function get($var)
181
+	{
182
+		$this->_dbLoadIfNotLoaded();
183
+
184
+		// Let's first check if the key exist.
185
+		if (!isset($this->dbRow[$var])) {
186
+			/*
187 187
             // Unable to find column.... this is an error if the object has been retrieved from database.
188 188
             // If it's a new object, well, that may not be an error after all!
189 189
             // Let's check if the column does exist in the table
@@ -203,34 +203,34 @@  discard block
 block discarded – undo
203 203
             $str = "Could not find column \"$var\" in table \"$this->dbTableName\". Maybe you meant one of those columns: '".implode("', '",$result_array)."'";
204 204
 
205 205
             throw new TDBMException($str);*/
206
-            return;
207
-        }
208
-
209
-        $value = $this->dbRow[$var];
210
-        if ($value instanceof \DateTime) {
211
-            return \DateTimeImmutable::createFromMutable($value);
212
-        }
213
-        return $this->dbRow[$var];
214
-    }
215
-
216
-    /**
217
-     * Returns true if a column is set, false otherwise.
218
-     *
219
-     * @param string $var
220
-     *
221
-     * @return bool
222
-     */
223
-    /*public function has($var) {
206
+			return;
207
+		}
208
+
209
+		$value = $this->dbRow[$var];
210
+		if ($value instanceof \DateTime) {
211
+			return \DateTimeImmutable::createFromMutable($value);
212
+		}
213
+		return $this->dbRow[$var];
214
+	}
215
+
216
+	/**
217
+	 * Returns true if a column is set, false otherwise.
218
+	 *
219
+	 * @param string $var
220
+	 *
221
+	 * @return bool
222
+	 */
223
+	/*public function has($var) {
224 224
         $this->_dbLoadIfNotLoaded();
225 225
 
226 226
         return isset($this->dbRow[$var]);
227 227
     }*/
228 228
 
229
-    public function set($var, $value)
230
-    {
231
-        $this->_dbLoadIfNotLoaded();
229
+	public function set($var, $value)
230
+	{
231
+		$this->_dbLoadIfNotLoaded();
232 232
 
233
-        /*
233
+		/*
234 234
         // Ok, let's start by checking the column type
235 235
         $type = $this->db_connection->getColumnType($this->dbTableName, $var);
236 236
 
@@ -240,184 +240,184 @@  discard block
 block discarded – undo
240 240
         }
241 241
         */
242 242
 
243
-        /*if ($var == $this->getPrimaryKey() && isset($this->dbRow[$var]))
243
+		/*if ($var == $this->getPrimaryKey() && isset($this->dbRow[$var]))
244 244
             throw new TDBMException("Error! Changing primary key value is forbidden.");*/
245
-        $this->dbRow[$var] = $value;
246
-        if ($this->tdbmService !== null && $this->status === TDBMObjectStateEnum::STATE_LOADED) {
247
-            $this->status = TDBMObjectStateEnum::STATE_DIRTY;
248
-            $this->tdbmService->_addToToSaveObjectList($this);
249
-        }
250
-    }
251
-
252
-    /**
253
-     * @param string             $foreignKeyName
254
-     * @param AbstractTDBMObject $bean
255
-     */
256
-    public function setRef($foreignKeyName, AbstractTDBMObject $bean = null)
257
-    {
258
-        $this->references[$foreignKeyName] = $bean;
259
-
260
-        if ($this->tdbmService !== null && $this->status === TDBMObjectStateEnum::STATE_LOADED) {
261
-            $this->status = TDBMObjectStateEnum::STATE_DIRTY;
262
-            $this->tdbmService->_addToToSaveObjectList($this);
263
-        }
264
-    }
265
-
266
-    /**
267
-     * @param string $foreignKeyName A unique name for this reference
268
-     *
269
-     * @return AbstractTDBMObject|null
270
-     */
271
-    public function getRef($foreignKeyName)
272
-    {
273
-        if (isset($this->references[$foreignKeyName])) {
274
-            return $this->references[$foreignKeyName];
275
-        } elseif ($this->status === TDBMObjectStateEnum::STATE_NEW) {
276
-            // If the object is new and has no property, then it has to be empty.
277
-            return;
278
-        } else {
279
-            $this->_dbLoadIfNotLoaded();
280
-
281
-            // Let's match the name of the columns to the primary key values
282
-            $fk = $this->tdbmService->_getForeignKeyByName($this->dbTableName, $foreignKeyName);
283
-
284
-            $values = [];
285
-            foreach ($fk->getLocalColumns() as $column) {
286
-                $values[] = $this->dbRow[$column];
287
-            }
288
-
289
-            $filter = array_combine($this->tdbmService->getPrimaryKeyColumns($fk->getForeignTableName()), $values);
290
-
291
-            return $this->tdbmService->findObjectByPk($fk->getForeignTableName(), $filter, [], true);
292
-        }
293
-    }
294
-
295
-    /**
296
-     * Returns the name of the table this object comes from.
297
-     *
298
-     * @return string
299
-     */
300
-    public function _getDbTableName()
301
-    {
302
-        return $this->dbTableName;
303
-    }
304
-
305
-    /**
306
-     * Method used internally by TDBM. You should not use it directly.
307
-     * This method returns the status of the TDBMObject.
308
-     * This is one of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED.
309
-     * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject.
310
-     * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet.
311
-     * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory.
312
-     *
313
-     * @return string
314
-     */
315
-    public function _getStatus()
316
-    {
317
-        return $this->status;
318
-    }
319
-
320
-    /**
321
-     * Override the native php clone function for TDBMObjects.
322
-     */
323
-    public function __clone()
324
-    {
325
-        // Let's load the row (before we lose the ID!)
326
-        $this->_dbLoadIfNotLoaded();
327
-
328
-        //Let's set the status to detached
329
-        $this->status = TDBMObjectStateEnum::STATE_DETACHED;
330
-
331
-        $this->primaryKeys = [];
332
-
333
-        //Now unset the PK from the row
334
-        if ($this->tdbmService) {
335
-            $pk_array = $this->tdbmService->getPrimaryKeyColumns($this->dbTableName);
336
-            foreach ($pk_array as $pk) {
337
-                $this->dbRow[$pk] = null;
338
-            }
339
-        }
340
-    }
341
-
342
-    /**
343
-     * Returns raw database row.
344
-     *
345
-     * @return array
346
-     */
347
-    public function _getDbRow()
348
-    {
349
-        // Let's merge $dbRow and $references
350
-        $dbRow = $this->dbRow;
351
-
352
-        foreach ($this->references as $foreignKeyName => $reference) {
353
-            // Let's match the name of the columns to the primary key values
354
-            $fk = $this->tdbmService->_getForeignKeyByName($this->dbTableName, $foreignKeyName);
355
-            $refDbRows = $reference->_getDbRows();
356
-            $firstRefDbRow = reset($refDbRows);
357
-            $pkValues = array_values($firstRefDbRow->_getPrimaryKeys());
358
-            $localColumns = $fk->getLocalColumns();
359
-
360
-            for ($i = 0, $count = count($localColumns); $i < $count; ++$i) {
361
-                $dbRow[$localColumns[$i]] = $pkValues[$i];
362
-            }
363
-        }
364
-
365
-        return $dbRow;
366
-    }
367
-
368
-    /**
369
-     * Returns references array.
370
-     *
371
-     * @return AbstractTDBMObject[]
372
-     */
373
-    public function _getReferences()
374
-    {
375
-        return $this->references;
376
-    }
377
-
378
-    /**
379
-     * Returns the values of the primary key.
380
-     * This is set when the object is in "loaded" state.
381
-     *
382
-     * @return array
383
-     */
384
-    public function _getPrimaryKeys()
385
-    {
386
-        return $this->primaryKeys;
387
-    }
388
-
389
-    /**
390
-     * Sets the values of the primary key.
391
-     * This is set when the object is in "loaded" state.
392
-     *
393
-     * @param array $primaryKeys
394
-     */
395
-    public function _setPrimaryKeys(array $primaryKeys)
396
-    {
397
-        $this->primaryKeys = $primaryKeys;
398
-        foreach ($this->primaryKeys as $column => $value) {
399
-            $this->dbRow[$column] = $value;
400
-        }
401
-    }
402
-
403
-    /**
404
-     * Returns the TDBMObject this bean is associated to.
405
-     *
406
-     * @return AbstractTDBMObject
407
-     */
408
-    public function getTDBMObject()
409
-    {
410
-        return $this->object;
411
-    }
412
-
413
-    /**
414
-     * Sets the TDBMObject this bean is associated to.
415
-     * Only used when cloning.
416
-     *
417
-     * @param AbstractTDBMObject $object
418
-     */
419
-    public function setTDBMObject(AbstractTDBMObject $object)
420
-    {
421
-        $this->object = $object;
422
-    }
245
+		$this->dbRow[$var] = $value;
246
+		if ($this->tdbmService !== null && $this->status === TDBMObjectStateEnum::STATE_LOADED) {
247
+			$this->status = TDBMObjectStateEnum::STATE_DIRTY;
248
+			$this->tdbmService->_addToToSaveObjectList($this);
249
+		}
250
+	}
251
+
252
+	/**
253
+	 * @param string             $foreignKeyName
254
+	 * @param AbstractTDBMObject $bean
255
+	 */
256
+	public function setRef($foreignKeyName, AbstractTDBMObject $bean = null)
257
+	{
258
+		$this->references[$foreignKeyName] = $bean;
259
+
260
+		if ($this->tdbmService !== null && $this->status === TDBMObjectStateEnum::STATE_LOADED) {
261
+			$this->status = TDBMObjectStateEnum::STATE_DIRTY;
262
+			$this->tdbmService->_addToToSaveObjectList($this);
263
+		}
264
+	}
265
+
266
+	/**
267
+	 * @param string $foreignKeyName A unique name for this reference
268
+	 *
269
+	 * @return AbstractTDBMObject|null
270
+	 */
271
+	public function getRef($foreignKeyName)
272
+	{
273
+		if (isset($this->references[$foreignKeyName])) {
274
+			return $this->references[$foreignKeyName];
275
+		} elseif ($this->status === TDBMObjectStateEnum::STATE_NEW) {
276
+			// If the object is new and has no property, then it has to be empty.
277
+			return;
278
+		} else {
279
+			$this->_dbLoadIfNotLoaded();
280
+
281
+			// Let's match the name of the columns to the primary key values
282
+			$fk = $this->tdbmService->_getForeignKeyByName($this->dbTableName, $foreignKeyName);
283
+
284
+			$values = [];
285
+			foreach ($fk->getLocalColumns() as $column) {
286
+				$values[] = $this->dbRow[$column];
287
+			}
288
+
289
+			$filter = array_combine($this->tdbmService->getPrimaryKeyColumns($fk->getForeignTableName()), $values);
290
+
291
+			return $this->tdbmService->findObjectByPk($fk->getForeignTableName(), $filter, [], true);
292
+		}
293
+	}
294
+
295
+	/**
296
+	 * Returns the name of the table this object comes from.
297
+	 *
298
+	 * @return string
299
+	 */
300
+	public function _getDbTableName()
301
+	{
302
+		return $this->dbTableName;
303
+	}
304
+
305
+	/**
306
+	 * Method used internally by TDBM. You should not use it directly.
307
+	 * This method returns the status of the TDBMObject.
308
+	 * This is one of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED.
309
+	 * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject.
310
+	 * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet.
311
+	 * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory.
312
+	 *
313
+	 * @return string
314
+	 */
315
+	public function _getStatus()
316
+	{
317
+		return $this->status;
318
+	}
319
+
320
+	/**
321
+	 * Override the native php clone function for TDBMObjects.
322
+	 */
323
+	public function __clone()
324
+	{
325
+		// Let's load the row (before we lose the ID!)
326
+		$this->_dbLoadIfNotLoaded();
327
+
328
+		//Let's set the status to detached
329
+		$this->status = TDBMObjectStateEnum::STATE_DETACHED;
330
+
331
+		$this->primaryKeys = [];
332
+
333
+		//Now unset the PK from the row
334
+		if ($this->tdbmService) {
335
+			$pk_array = $this->tdbmService->getPrimaryKeyColumns($this->dbTableName);
336
+			foreach ($pk_array as $pk) {
337
+				$this->dbRow[$pk] = null;
338
+			}
339
+		}
340
+	}
341
+
342
+	/**
343
+	 * Returns raw database row.
344
+	 *
345
+	 * @return array
346
+	 */
347
+	public function _getDbRow()
348
+	{
349
+		// Let's merge $dbRow and $references
350
+		$dbRow = $this->dbRow;
351
+
352
+		foreach ($this->references as $foreignKeyName => $reference) {
353
+			// Let's match the name of the columns to the primary key values
354
+			$fk = $this->tdbmService->_getForeignKeyByName($this->dbTableName, $foreignKeyName);
355
+			$refDbRows = $reference->_getDbRows();
356
+			$firstRefDbRow = reset($refDbRows);
357
+			$pkValues = array_values($firstRefDbRow->_getPrimaryKeys());
358
+			$localColumns = $fk->getLocalColumns();
359
+
360
+			for ($i = 0, $count = count($localColumns); $i < $count; ++$i) {
361
+				$dbRow[$localColumns[$i]] = $pkValues[$i];
362
+			}
363
+		}
364
+
365
+		return $dbRow;
366
+	}
367
+
368
+	/**
369
+	 * Returns references array.
370
+	 *
371
+	 * @return AbstractTDBMObject[]
372
+	 */
373
+	public function _getReferences()
374
+	{
375
+		return $this->references;
376
+	}
377
+
378
+	/**
379
+	 * Returns the values of the primary key.
380
+	 * This is set when the object is in "loaded" state.
381
+	 *
382
+	 * @return array
383
+	 */
384
+	public function _getPrimaryKeys()
385
+	{
386
+		return $this->primaryKeys;
387
+	}
388
+
389
+	/**
390
+	 * Sets the values of the primary key.
391
+	 * This is set when the object is in "loaded" state.
392
+	 *
393
+	 * @param array $primaryKeys
394
+	 */
395
+	public function _setPrimaryKeys(array $primaryKeys)
396
+	{
397
+		$this->primaryKeys = $primaryKeys;
398
+		foreach ($this->primaryKeys as $column => $value) {
399
+			$this->dbRow[$column] = $value;
400
+		}
401
+	}
402
+
403
+	/**
404
+	 * Returns the TDBMObject this bean is associated to.
405
+	 *
406
+	 * @return AbstractTDBMObject
407
+	 */
408
+	public function getTDBMObject()
409
+	{
410
+		return $this->object;
411
+	}
412
+
413
+	/**
414
+	 * Sets the TDBMObject this bean is associated to.
415
+	 * Only used when cloning.
416
+	 *
417
+	 * @param AbstractTDBMObject $object
418
+	 */
419
+	public function setTDBMObject(AbstractTDBMObject $object)
420
+	{
421
+		$this->object = $object;
422
+	}
423 423
 }
Please login to merge, or discard this patch.