Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Connection often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Connection, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | abstract class Connection implements ActiveRecordInterface |
||
35 | { |
||
36 | /** |
||
37 | * TableMap class name |
||
38 | */ |
||
39 | const TABLE_MAP = '\\Jalle19\\StatusManager\\Database\\Map\\ConnectionTableMap'; |
||
40 | |||
41 | |||
42 | /** |
||
43 | * attribute to determine if this object has previously been saved. |
||
44 | * @var boolean |
||
45 | */ |
||
46 | protected $new = true; |
||
47 | |||
48 | /** |
||
49 | * attribute to determine whether this object has been deleted. |
||
50 | * @var boolean |
||
51 | */ |
||
52 | protected $deleted = false; |
||
53 | |||
54 | /** |
||
55 | * The columns that have been modified in current object. |
||
56 | * Tracking modified columns allows us to only update modified columns. |
||
57 | * @var array |
||
58 | */ |
||
59 | protected $modifiedColumns = array(); |
||
60 | |||
61 | /** |
||
62 | * The (virtual) columns that are added at runtime |
||
63 | * The formatters can add supplementary columns based on a resultset |
||
64 | * @var array |
||
65 | */ |
||
66 | protected $virtualColumns = array(); |
||
67 | |||
68 | /** |
||
69 | * The value for the id field. |
||
70 | * |
||
71 | * @var int |
||
72 | */ |
||
73 | protected $id; |
||
74 | |||
75 | /** |
||
76 | * The value for the instance_name field. |
||
77 | * |
||
78 | * @var string |
||
79 | */ |
||
80 | protected $instance_name; |
||
81 | |||
82 | /** |
||
83 | * The value for the user_id field. |
||
84 | * |
||
85 | * @var int |
||
86 | */ |
||
87 | protected $user_id; |
||
88 | |||
89 | /** |
||
90 | * The value for the peer field. |
||
91 | * |
||
92 | * @var string |
||
93 | */ |
||
94 | protected $peer; |
||
95 | |||
96 | /** |
||
97 | * The value for the started field. |
||
98 | * |
||
99 | * @var \DateTime |
||
100 | */ |
||
101 | protected $started; |
||
102 | |||
103 | /** |
||
104 | * The value for the type field. |
||
105 | * |
||
106 | * @var string |
||
107 | */ |
||
108 | protected $type; |
||
109 | |||
110 | /** |
||
111 | * @var ChildInstance |
||
112 | */ |
||
113 | protected $aInstance; |
||
114 | |||
115 | /** |
||
116 | * @var ChildUser |
||
117 | */ |
||
118 | protected $aUser; |
||
119 | |||
120 | /** |
||
121 | * Flag to prevent endless save loop, if this object is referenced |
||
122 | * by another object which falls in this transaction. |
||
123 | * |
||
124 | * @var boolean |
||
125 | */ |
||
126 | protected $alreadyInSave = false; |
||
127 | |||
128 | /** |
||
129 | * Initializes internal state of Jalle19\StatusManager\Database\Base\Connection object. |
||
130 | */ |
||
131 | public function __construct() |
||
132 | { |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * Returns whether the object has been modified. |
||
137 | * |
||
138 | * @return boolean True if the object has been modified. |
||
139 | */ |
||
140 | public function isModified() |
||
141 | { |
||
142 | return !!$this->modifiedColumns; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Has specified column been modified? |
||
147 | * |
||
148 | * @param string $col column fully qualified name (TableMap::TYPE_COLNAME), e.g. Book::AUTHOR_ID |
||
149 | * @return boolean True if $col has been modified. |
||
150 | */ |
||
151 | public function isColumnModified($col) |
||
152 | { |
||
153 | return $this->modifiedColumns && isset($this->modifiedColumns[$col]); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Get the columns that have been modified in this object. |
||
158 | * @return array A unique list of the modified column names for this object. |
||
159 | */ |
||
160 | public function getModifiedColumns() |
||
161 | { |
||
162 | return $this->modifiedColumns ? array_keys($this->modifiedColumns) : []; |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * Returns whether the object has ever been saved. This will |
||
167 | * be false, if the object was retrieved from storage or was created |
||
168 | * and then saved. |
||
169 | * |
||
170 | * @return boolean true, if the object has never been persisted. |
||
171 | */ |
||
172 | public function isNew() |
||
173 | { |
||
174 | return $this->new; |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Setter for the isNew attribute. This method will be called |
||
179 | * by Propel-generated children and objects. |
||
180 | * |
||
181 | * @param boolean $b the state of the object. |
||
182 | */ |
||
183 | public function setNew($b) |
||
184 | { |
||
185 | $this->new = (boolean) $b; |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Whether this object has been deleted. |
||
190 | * @return boolean The deleted state of this object. |
||
191 | */ |
||
192 | public function isDeleted() |
||
193 | { |
||
194 | return $this->deleted; |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * Specify whether this object has been deleted. |
||
199 | * @param boolean $b The deleted state of this object. |
||
200 | * @return void |
||
201 | */ |
||
202 | public function setDeleted($b) |
||
203 | { |
||
204 | $this->deleted = (boolean) $b; |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * Sets the modified state for the object to be false. |
||
209 | * @param string $col If supplied, only the specified column is reset. |
||
210 | * @return void |
||
211 | */ |
||
212 | public function resetModified($col = null) |
||
213 | { |
||
214 | if (null !== $col) { |
||
215 | if (isset($this->modifiedColumns[$col])) { |
||
216 | unset($this->modifiedColumns[$col]); |
||
217 | } |
||
218 | } else { |
||
219 | $this->modifiedColumns = array(); |
||
220 | } |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * Compares this with another <code>Connection</code> instance. If |
||
225 | * <code>obj</code> is an instance of <code>Connection</code>, delegates to |
||
226 | * <code>equals(Connection)</code>. Otherwise, returns <code>false</code>. |
||
227 | * |
||
228 | * @param mixed $obj The object to compare to. |
||
229 | * @return boolean Whether equal to the object specified. |
||
230 | */ |
||
231 | public function equals($obj) |
||
232 | { |
||
233 | if (!$obj instanceof static) { |
||
234 | return false; |
||
235 | } |
||
236 | |||
237 | if ($this === $obj) { |
||
238 | return true; |
||
239 | } |
||
240 | |||
241 | if (null === $this->getPrimaryKey() || null === $obj->getPrimaryKey()) { |
||
242 | return false; |
||
243 | } |
||
244 | |||
245 | return $this->getPrimaryKey() === $obj->getPrimaryKey(); |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * Get the associative array of the virtual columns in this object |
||
250 | * |
||
251 | * @return array |
||
252 | */ |
||
253 | public function getVirtualColumns() |
||
254 | { |
||
255 | return $this->virtualColumns; |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * Checks the existence of a virtual column in this object |
||
260 | * |
||
261 | * @param string $name The virtual column name |
||
262 | * @return boolean |
||
263 | */ |
||
264 | public function hasVirtualColumn($name) |
||
265 | { |
||
266 | return array_key_exists($name, $this->virtualColumns); |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * Get the value of a virtual column in this object |
||
271 | * |
||
272 | * @param string $name The virtual column name |
||
273 | * @return mixed |
||
274 | * |
||
275 | * @throws PropelException |
||
276 | */ |
||
277 | public function getVirtualColumn($name) |
||
278 | { |
||
279 | if (!$this->hasVirtualColumn($name)) { |
||
280 | throw new PropelException(sprintf('Cannot get value of inexistent virtual column %s.', $name)); |
||
281 | } |
||
282 | |||
283 | return $this->virtualColumns[$name]; |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * Set the value of a virtual column in this object |
||
288 | * |
||
289 | * @param string $name The virtual column name |
||
290 | * @param mixed $value The value to give to the virtual column |
||
291 | * |
||
292 | * @return $this|Connection The current object, for fluid interface |
||
293 | */ |
||
294 | public function setVirtualColumn($name, $value) |
||
295 | { |
||
296 | $this->virtualColumns[$name] = $value; |
||
297 | |||
298 | return $this; |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * Logs a message using Propel::log(). |
||
303 | * |
||
304 | * @param string $msg |
||
305 | * @param int $priority One of the Propel::LOG_* logging levels |
||
306 | * @return boolean |
||
307 | */ |
||
308 | protected function log($msg, $priority = Propel::LOG_INFO) |
||
309 | { |
||
310 | return Propel::log(get_class($this) . ': ' . $msg, $priority); |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * Export the current object properties to a string, using a given parser format |
||
315 | * <code> |
||
316 | * $book = BookQuery::create()->findPk(9012); |
||
317 | * echo $book->exportTo('JSON'); |
||
318 | * => {"Id":9012,"Title":"Don Juan","ISBN":"0140422161","Price":12.99,"PublisherId":1234,"AuthorId":5678}'); |
||
319 | * </code> |
||
320 | * |
||
321 | * @param mixed $parser A AbstractParser instance, or a format name ('XML', 'YAML', 'JSON', 'CSV') |
||
322 | * @param boolean $includeLazyLoadColumns (optional) Whether to include lazy load(ed) columns. Defaults to TRUE. |
||
323 | * @return string The exported data |
||
324 | */ |
||
325 | public function exportTo($parser, $includeLazyLoadColumns = true) |
||
326 | { |
||
327 | if (!$parser instanceof AbstractParser) { |
||
328 | $parser = AbstractParser::getParser($parser); |
||
329 | } |
||
330 | |||
331 | return $parser->fromArray($this->toArray(TableMap::TYPE_PHPNAME, $includeLazyLoadColumns, array(), true)); |
||
332 | } |
||
333 | |||
334 | /** |
||
335 | * Clean up internal collections prior to serializing |
||
336 | * Avoids recursive loops that turn into segmentation faults when serializing |
||
337 | */ |
||
338 | public function __sleep() |
||
339 | { |
||
340 | $this->clearAllReferences(); |
||
341 | |||
342 | $cls = new \ReflectionClass($this); |
||
343 | $propertyNames = []; |
||
344 | $serializableProperties = array_diff($cls->getProperties(), $cls->getProperties(\ReflectionProperty::IS_STATIC)); |
||
345 | |||
346 | foreach($serializableProperties as $property) { |
||
347 | $propertyNames[] = $property->getName(); |
||
348 | } |
||
349 | |||
350 | return $propertyNames; |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * Get the [id] column value. |
||
355 | * |
||
356 | * @return int |
||
357 | */ |
||
358 | public function getId() |
||
359 | { |
||
360 | return $this->id; |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * Get the [instance_name] column value. |
||
365 | * |
||
366 | * @return string |
||
367 | */ |
||
368 | public function getInstanceName() |
||
369 | { |
||
370 | return $this->instance_name; |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * Get the [user_id] column value. |
||
375 | * |
||
376 | * @return int |
||
377 | */ |
||
378 | public function getUserId() |
||
379 | { |
||
380 | return $this->user_id; |
||
381 | } |
||
382 | |||
383 | /** |
||
384 | * Get the [peer] column value. |
||
385 | * |
||
386 | * @return string |
||
387 | */ |
||
388 | public function getPeer() |
||
389 | { |
||
390 | return $this->peer; |
||
391 | } |
||
392 | |||
393 | /** |
||
394 | * Get the [optionally formatted] temporal [started] column value. |
||
395 | * |
||
396 | * |
||
397 | * @param string $format The date/time format string (either date()-style or strftime()-style). |
||
398 | * If format is NULL, then the raw DateTime object will be returned. |
||
399 | * |
||
400 | * @return string|DateTime Formatted date/time value as string or DateTime object (if format is NULL), NULL if column is NULL |
||
401 | * |
||
402 | * @throws PropelException - if unable to parse/validate the date/time value. |
||
403 | */ |
||
404 | public function getStarted($format = NULL) |
||
405 | { |
||
406 | if ($format === null) { |
||
407 | return $this->started; |
||
408 | } else { |
||
409 | return $this->started instanceof \DateTime ? $this->started->format($format) : null; |
||
410 | } |
||
411 | } |
||
412 | |||
413 | /** |
||
414 | * Get the [type] column value. |
||
415 | * |
||
416 | * @return string |
||
417 | */ |
||
418 | public function getType() |
||
419 | { |
||
420 | return $this->type; |
||
421 | } |
||
422 | |||
423 | /** |
||
424 | * Set the value of [id] column. |
||
425 | * |
||
426 | * @param int $v new value |
||
427 | * @return $this|\Jalle19\StatusManager\Database\Connection The current object (for fluent API support) |
||
428 | */ |
||
429 | public function setId($v) |
||
430 | { |
||
431 | if ($v !== null) { |
||
432 | $v = (int) $v; |
||
433 | } |
||
434 | |||
435 | if ($this->id !== $v) { |
||
436 | $this->id = $v; |
||
437 | $this->modifiedColumns[ConnectionTableMap::COL_ID] = true; |
||
438 | } |
||
439 | |||
440 | return $this; |
||
441 | } // setId() |
||
442 | |||
443 | /** |
||
444 | * Set the value of [instance_name] column. |
||
445 | * |
||
446 | * @param string $v new value |
||
447 | * @return $this|\Jalle19\StatusManager\Database\Connection The current object (for fluent API support) |
||
448 | */ |
||
449 | public function setInstanceName($v) |
||
450 | { |
||
451 | if ($v !== null) { |
||
452 | $v = (string) $v; |
||
453 | } |
||
454 | |||
455 | if ($this->instance_name !== $v) { |
||
456 | $this->instance_name = $v; |
||
457 | $this->modifiedColumns[ConnectionTableMap::COL_INSTANCE_NAME] = true; |
||
458 | } |
||
459 | |||
460 | if ($this->aInstance !== null && $this->aInstance->getName() !== $v) { |
||
461 | $this->aInstance = null; |
||
462 | } |
||
463 | |||
464 | return $this; |
||
465 | } // setInstanceName() |
||
466 | |||
467 | /** |
||
468 | * Set the value of [user_id] column. |
||
469 | * |
||
470 | * @param int $v new value |
||
471 | * @return $this|\Jalle19\StatusManager\Database\Connection The current object (for fluent API support) |
||
472 | */ |
||
473 | public function setUserId($v) |
||
474 | { |
||
475 | if ($v !== null) { |
||
476 | $v = (int) $v; |
||
477 | } |
||
478 | |||
479 | if ($this->user_id !== $v) { |
||
480 | $this->user_id = $v; |
||
481 | $this->modifiedColumns[ConnectionTableMap::COL_USER_ID] = true; |
||
482 | } |
||
483 | |||
484 | if ($this->aUser !== null && $this->aUser->getId() !== $v) { |
||
485 | $this->aUser = null; |
||
486 | } |
||
487 | |||
488 | return $this; |
||
489 | } // setUserId() |
||
490 | |||
491 | /** |
||
492 | * Set the value of [peer] column. |
||
493 | * |
||
494 | * @param string $v new value |
||
495 | * @return $this|\Jalle19\StatusManager\Database\Connection The current object (for fluent API support) |
||
496 | */ |
||
497 | public function setPeer($v) |
||
498 | { |
||
499 | if ($v !== null) { |
||
500 | $v = (string) $v; |
||
501 | } |
||
502 | |||
503 | if ($this->peer !== $v) { |
||
504 | $this->peer = $v; |
||
505 | $this->modifiedColumns[ConnectionTableMap::COL_PEER] = true; |
||
506 | } |
||
507 | |||
508 | return $this; |
||
509 | } // setPeer() |
||
510 | |||
511 | /** |
||
512 | * Sets the value of [started] column to a normalized version of the date/time value specified. |
||
513 | * |
||
514 | * @param mixed $v string, integer (timestamp), or \DateTime value. |
||
515 | * Empty strings are treated as NULL. |
||
516 | * @return $this|\Jalle19\StatusManager\Database\Connection The current object (for fluent API support) |
||
517 | */ |
||
518 | public function setStarted($v) |
||
519 | { |
||
520 | $dt = PropelDateTime::newInstance($v, null, 'DateTime'); |
||
521 | if ($this->started !== null || $dt !== null) { |
||
522 | if ($this->started === null || $dt === null || $dt->format("Y-m-d H:i:s") !== $this->started->format("Y-m-d H:i:s")) { |
||
523 | $this->started = $dt === null ? null : clone $dt; |
||
524 | $this->modifiedColumns[ConnectionTableMap::COL_STARTED] = true; |
||
525 | } |
||
526 | } // if either are not null |
||
527 | |||
528 | return $this; |
||
529 | } // setStarted() |
||
530 | |||
531 | /** |
||
532 | * Set the value of [type] column. |
||
533 | * |
||
534 | * @param string $v new value |
||
535 | * @return $this|\Jalle19\StatusManager\Database\Connection The current object (for fluent API support) |
||
536 | */ |
||
537 | public function setType($v) |
||
538 | { |
||
539 | if ($v !== null) { |
||
540 | $v = (string) $v; |
||
541 | } |
||
542 | |||
543 | if ($this->type !== $v) { |
||
544 | $this->type = $v; |
||
545 | $this->modifiedColumns[ConnectionTableMap::COL_TYPE] = true; |
||
546 | } |
||
547 | |||
548 | return $this; |
||
549 | } // setType() |
||
550 | |||
551 | /** |
||
552 | * Indicates whether the columns in this object are only set to default values. |
||
553 | * |
||
554 | * This method can be used in conjunction with isModified() to indicate whether an object is both |
||
555 | * modified _and_ has some values set which are non-default. |
||
556 | * |
||
557 | * @return boolean Whether the columns in this object are only been set with default values. |
||
558 | */ |
||
559 | public function hasOnlyDefaultValues() |
||
560 | { |
||
561 | // otherwise, everything was equal, so return TRUE |
||
562 | return true; |
||
563 | } // hasOnlyDefaultValues() |
||
564 | |||
565 | /** |
||
566 | * Hydrates (populates) the object variables with values from the database resultset. |
||
567 | * |
||
568 | * An offset (0-based "start column") is specified so that objects can be hydrated |
||
569 | * with a subset of the columns in the resultset rows. This is needed, for example, |
||
570 | * for results of JOIN queries where the resultset row includes columns from two or |
||
571 | * more tables. |
||
572 | * |
||
573 | * @param array $row The row returned by DataFetcher->fetch(). |
||
574 | * @param int $startcol 0-based offset column which indicates which restultset column to start with. |
||
575 | * @param boolean $rehydrate Whether this object is being re-hydrated from the database. |
||
576 | * @param string $indexType The index type of $row. Mostly DataFetcher->getIndexType(). |
||
577 | One of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_CAMELNAME |
||
578 | * TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM. |
||
579 | * |
||
580 | * @return int next starting column |
||
581 | * @throws PropelException - Any caught Exception will be rewrapped as a PropelException. |
||
582 | */ |
||
583 | public function hydrate($row, $startcol = 0, $rehydrate = false, $indexType = TableMap::TYPE_NUM) |
||
584 | { |
||
585 | try { |
||
586 | |||
587 | $col = $row[TableMap::TYPE_NUM == $indexType ? 0 + $startcol : ConnectionTableMap::translateFieldName('Id', TableMap::TYPE_PHPNAME, $indexType)]; |
||
588 | $this->id = (null !== $col) ? (int) $col : null; |
||
589 | |||
590 | $col = $row[TableMap::TYPE_NUM == $indexType ? 1 + $startcol : ConnectionTableMap::translateFieldName('InstanceName', TableMap::TYPE_PHPNAME, $indexType)]; |
||
591 | $this->instance_name = (null !== $col) ? (string) $col : null; |
||
592 | |||
593 | $col = $row[TableMap::TYPE_NUM == $indexType ? 2 + $startcol : ConnectionTableMap::translateFieldName('UserId', TableMap::TYPE_PHPNAME, $indexType)]; |
||
594 | $this->user_id = (null !== $col) ? (int) $col : null; |
||
595 | |||
596 | $col = $row[TableMap::TYPE_NUM == $indexType ? 3 + $startcol : ConnectionTableMap::translateFieldName('Peer', TableMap::TYPE_PHPNAME, $indexType)]; |
||
597 | $this->peer = (null !== $col) ? (string) $col : null; |
||
598 | |||
599 | $col = $row[TableMap::TYPE_NUM == $indexType ? 4 + $startcol : ConnectionTableMap::translateFieldName('Started', TableMap::TYPE_PHPNAME, $indexType)]; |
||
600 | $this->started = (null !== $col) ? PropelDateTime::newInstance($col, null, 'DateTime') : null; |
||
601 | |||
602 | $col = $row[TableMap::TYPE_NUM == $indexType ? 5 + $startcol : ConnectionTableMap::translateFieldName('Type', TableMap::TYPE_PHPNAME, $indexType)]; |
||
603 | $this->type = (null !== $col) ? (string) $col : null; |
||
604 | $this->resetModified(); |
||
605 | |||
606 | $this->setNew(false); |
||
607 | |||
608 | if ($rehydrate) { |
||
609 | $this->ensureConsistency(); |
||
610 | } |
||
611 | |||
612 | return $startcol + 6; // 6 = ConnectionTableMap::NUM_HYDRATE_COLUMNS. |
||
613 | |||
614 | } catch (Exception $e) { |
||
615 | throw new PropelException(sprintf('Error populating %s object', '\\Jalle19\\StatusManager\\Database\\Connection'), 0, $e); |
||
616 | } |
||
617 | } |
||
618 | |||
619 | /** |
||
620 | * Checks and repairs the internal consistency of the object. |
||
621 | * |
||
622 | * This method is executed after an already-instantiated object is re-hydrated |
||
623 | * from the database. It exists to check any foreign keys to make sure that |
||
624 | * the objects related to the current object are correct based on foreign key. |
||
625 | * |
||
626 | * You can override this method in the stub class, but you should always invoke |
||
627 | * the base method from the overridden method (i.e. parent::ensureConsistency()), |
||
628 | * in case your model changes. |
||
629 | * |
||
630 | * @throws PropelException |
||
631 | */ |
||
632 | public function ensureConsistency() |
||
633 | { |
||
634 | if ($this->aInstance !== null && $this->instance_name !== $this->aInstance->getName()) { |
||
635 | $this->aInstance = null; |
||
636 | } |
||
637 | if ($this->aUser !== null && $this->user_id !== $this->aUser->getId()) { |
||
638 | $this->aUser = null; |
||
639 | } |
||
640 | } // ensureConsistency |
||
641 | |||
642 | /** |
||
643 | * Reloads this object from datastore based on primary key and (optionally) resets all associated objects. |
||
644 | * |
||
645 | * This will only work if the object has been saved and has a valid primary key set. |
||
646 | * |
||
647 | * @param boolean $deep (optional) Whether to also de-associated any related objects. |
||
648 | * @param ConnectionInterface $con (optional) The ConnectionInterface connection to use. |
||
649 | * @return void |
||
650 | * @throws PropelException - if this object is deleted, unsaved or doesn't have pk match in db |
||
651 | */ |
||
652 | public function reload($deep = false, ConnectionInterface $con = null) |
||
653 | { |
||
654 | if ($this->isDeleted()) { |
||
655 | throw new PropelException("Cannot reload a deleted object."); |
||
656 | } |
||
657 | |||
658 | if ($this->isNew()) { |
||
659 | throw new PropelException("Cannot reload an unsaved object."); |
||
660 | } |
||
661 | |||
662 | if ($con === null) { |
||
663 | $con = Propel::getServiceContainer()->getReadConnection(ConnectionTableMap::DATABASE_NAME); |
||
664 | } |
||
665 | |||
666 | // We don't need to alter the object instance pool; we're just modifying this instance |
||
667 | // already in the pool. |
||
668 | |||
669 | $dataFetcher = ChildConnectionQuery::create(null, $this->buildPkeyCriteria())->setFormatter(ModelCriteria::FORMAT_STATEMENT)->find($con); |
||
670 | $row = $dataFetcher->fetch(); |
||
671 | $dataFetcher->close(); |
||
672 | if (!$row) { |
||
673 | throw new PropelException('Cannot find matching row in the database to reload object values.'); |
||
674 | } |
||
675 | $this->hydrate($row, 0, true, $dataFetcher->getIndexType()); // rehydrate |
||
676 | |||
677 | if ($deep) { // also de-associate any related objects? |
||
678 | |||
679 | $this->aInstance = null; |
||
680 | $this->aUser = null; |
||
681 | } // if (deep) |
||
682 | } |
||
683 | |||
684 | /** |
||
685 | * Removes this object from datastore and sets delete attribute. |
||
686 | * |
||
687 | * @param ConnectionInterface $con |
||
688 | * @return void |
||
689 | * @throws PropelException |
||
690 | * @see Connection::setDeleted() |
||
691 | * @see Connection::isDeleted() |
||
692 | */ |
||
693 | public function delete(ConnectionInterface $con = null) |
||
694 | { |
||
695 | if ($this->isDeleted()) { |
||
696 | throw new PropelException("This object has already been deleted."); |
||
697 | } |
||
698 | |||
699 | if ($con === null) { |
||
700 | $con = Propel::getServiceContainer()->getWriteConnection(ConnectionTableMap::DATABASE_NAME); |
||
701 | } |
||
702 | |||
703 | $con->transaction(function () use ($con) { |
||
704 | $deleteQuery = ChildConnectionQuery::create() |
||
705 | ->filterByPrimaryKey($this->getPrimaryKey()); |
||
706 | $ret = $this->preDelete($con); |
||
707 | if ($ret) { |
||
708 | $deleteQuery->delete($con); |
||
709 | $this->postDelete($con); |
||
710 | $this->setDeleted(true); |
||
711 | } |
||
712 | }); |
||
713 | } |
||
714 | |||
715 | /** |
||
716 | * Persists this object to the database. |
||
717 | * |
||
718 | * If the object is new, it inserts it; otherwise an update is performed. |
||
719 | * All modified related objects will also be persisted in the doSave() |
||
720 | * method. This method wraps all precipitate database operations in a |
||
721 | * single transaction. |
||
722 | * |
||
723 | * @param ConnectionInterface $con |
||
724 | * @return int The number of rows affected by this insert/update and any referring fk objects' save() operations. |
||
725 | * @throws PropelException |
||
726 | * @see doSave() |
||
727 | */ |
||
728 | public function save(ConnectionInterface $con = null) |
||
729 | { |
||
730 | if ($this->isDeleted()) { |
||
731 | throw new PropelException("You cannot save an object that has been deleted."); |
||
732 | } |
||
733 | |||
734 | if ($con === null) { |
||
735 | $con = Propel::getServiceContainer()->getWriteConnection(ConnectionTableMap::DATABASE_NAME); |
||
736 | } |
||
737 | |||
738 | return $con->transaction(function () use ($con) { |
||
739 | $isInsert = $this->isNew(); |
||
740 | $ret = $this->preSave($con); |
||
741 | if ($isInsert) { |
||
742 | $ret = $ret && $this->preInsert($con); |
||
743 | } else { |
||
744 | $ret = $ret && $this->preUpdate($con); |
||
745 | } |
||
746 | if ($ret) { |
||
747 | $affectedRows = $this->doSave($con); |
||
748 | if ($isInsert) { |
||
749 | $this->postInsert($con); |
||
750 | } else { |
||
751 | $this->postUpdate($con); |
||
752 | } |
||
753 | $this->postSave($con); |
||
754 | ConnectionTableMap::addInstanceToPool($this); |
||
755 | } else { |
||
756 | $affectedRows = 0; |
||
757 | } |
||
758 | |||
759 | return $affectedRows; |
||
760 | }); |
||
761 | } |
||
762 | |||
763 | /** |
||
764 | * Performs the work of inserting or updating the row in the database. |
||
765 | * |
||
766 | * If the object is new, it inserts it; otherwise an update is performed. |
||
767 | * All related objects are also updated in this method. |
||
768 | * |
||
769 | * @param ConnectionInterface $con |
||
770 | * @return int The number of rows affected by this insert/update and any referring fk objects' save() operations. |
||
771 | * @throws PropelException |
||
772 | * @see save() |
||
773 | */ |
||
774 | protected function doSave(ConnectionInterface $con) |
||
775 | { |
||
776 | $affectedRows = 0; // initialize var to track total num of affected rows |
||
777 | if (!$this->alreadyInSave) { |
||
778 | $this->alreadyInSave = true; |
||
779 | |||
780 | // We call the save method on the following object(s) if they |
||
781 | // were passed to this object by their corresponding set |
||
782 | // method. This object relates to these object(s) by a |
||
783 | // foreign key reference. |
||
784 | |||
785 | if ($this->aInstance !== null) { |
||
786 | if ($this->aInstance->isModified() || $this->aInstance->isNew()) { |
||
787 | $affectedRows += $this->aInstance->save($con); |
||
788 | } |
||
789 | $this->setInstance($this->aInstance); |
||
790 | } |
||
791 | |||
792 | if ($this->aUser !== null) { |
||
793 | if ($this->aUser->isModified() || $this->aUser->isNew()) { |
||
794 | $affectedRows += $this->aUser->save($con); |
||
795 | } |
||
796 | $this->setUser($this->aUser); |
||
797 | } |
||
798 | |||
799 | if ($this->isNew() || $this->isModified()) { |
||
800 | // persist changes |
||
801 | if ($this->isNew()) { |
||
802 | $this->doInsert($con); |
||
803 | $affectedRows += 1; |
||
804 | } else { |
||
805 | $affectedRows += $this->doUpdate($con); |
||
806 | } |
||
807 | $this->resetModified(); |
||
808 | } |
||
809 | |||
810 | $this->alreadyInSave = false; |
||
811 | |||
812 | } |
||
813 | |||
814 | return $affectedRows; |
||
815 | } // doSave() |
||
816 | |||
817 | /** |
||
818 | * Insert the row in the database. |
||
819 | * |
||
820 | * @param ConnectionInterface $con |
||
821 | * |
||
822 | * @throws PropelException |
||
823 | * @see doSave() |
||
824 | */ |
||
825 | protected function doInsert(ConnectionInterface $con) |
||
826 | { |
||
827 | $modifiedColumns = array(); |
||
828 | $index = 0; |
||
829 | |||
830 | $this->modifiedColumns[ConnectionTableMap::COL_ID] = true; |
||
831 | if (null !== $this->id) { |
||
832 | throw new PropelException('Cannot insert a value for auto-increment primary key (' . ConnectionTableMap::COL_ID . ')'); |
||
833 | } |
||
834 | |||
835 | // check the columns in natural order for more readable SQL queries |
||
836 | if ($this->isColumnModified(ConnectionTableMap::COL_ID)) { |
||
837 | $modifiedColumns[':p' . $index++] = 'id'; |
||
838 | } |
||
839 | if ($this->isColumnModified(ConnectionTableMap::COL_INSTANCE_NAME)) { |
||
840 | $modifiedColumns[':p' . $index++] = 'instance_name'; |
||
841 | } |
||
842 | if ($this->isColumnModified(ConnectionTableMap::COL_USER_ID)) { |
||
843 | $modifiedColumns[':p' . $index++] = 'user_id'; |
||
844 | } |
||
845 | if ($this->isColumnModified(ConnectionTableMap::COL_PEER)) { |
||
846 | $modifiedColumns[':p' . $index++] = 'peer'; |
||
847 | } |
||
848 | if ($this->isColumnModified(ConnectionTableMap::COL_STARTED)) { |
||
849 | $modifiedColumns[':p' . $index++] = 'started'; |
||
850 | } |
||
851 | if ($this->isColumnModified(ConnectionTableMap::COL_TYPE)) { |
||
852 | $modifiedColumns[':p' . $index++] = 'type'; |
||
853 | } |
||
854 | |||
855 | $sql = sprintf( |
||
856 | 'INSERT INTO connection (%s) VALUES (%s)', |
||
857 | implode(', ', $modifiedColumns), |
||
858 | implode(', ', array_keys($modifiedColumns)) |
||
859 | ); |
||
860 | |||
861 | try { |
||
862 | $stmt = $con->prepare($sql); |
||
863 | foreach ($modifiedColumns as $identifier => $columnName) { |
||
864 | switch ($columnName) { |
||
865 | case 'id': |
||
866 | $stmt->bindValue($identifier, $this->id, PDO::PARAM_INT); |
||
867 | break; |
||
868 | case 'instance_name': |
||
869 | $stmt->bindValue($identifier, $this->instance_name, PDO::PARAM_STR); |
||
870 | break; |
||
871 | case 'user_id': |
||
872 | $stmt->bindValue($identifier, $this->user_id, PDO::PARAM_INT); |
||
873 | break; |
||
874 | case 'peer': |
||
875 | $stmt->bindValue($identifier, $this->peer, PDO::PARAM_STR); |
||
876 | break; |
||
877 | case 'started': |
||
878 | $stmt->bindValue($identifier, $this->started ? $this->started->format("Y-m-d H:i:s") : null, PDO::PARAM_STR); |
||
879 | break; |
||
880 | case 'type': |
||
881 | $stmt->bindValue($identifier, $this->type, PDO::PARAM_STR); |
||
882 | break; |
||
883 | } |
||
884 | } |
||
885 | $stmt->execute(); |
||
886 | } catch (Exception $e) { |
||
887 | Propel::log($e->getMessage(), Propel::LOG_ERR); |
||
888 | throw new PropelException(sprintf('Unable to execute INSERT statement [%s]', $sql), 0, $e); |
||
889 | } |
||
890 | |||
891 | try { |
||
892 | $pk = $con->lastInsertId(); |
||
893 | } catch (Exception $e) { |
||
894 | throw new PropelException('Unable to get autoincrement id.', 0, $e); |
||
895 | } |
||
896 | $this->setId($pk); |
||
897 | |||
898 | $this->setNew(false); |
||
899 | } |
||
900 | |||
901 | /** |
||
902 | * Update the row in the database. |
||
903 | * |
||
904 | * @param ConnectionInterface $con |
||
905 | * |
||
906 | * @return Integer Number of updated rows |
||
907 | * @see doSave() |
||
908 | */ |
||
909 | protected function doUpdate(ConnectionInterface $con) |
||
910 | { |
||
911 | $selectCriteria = $this->buildPkeyCriteria(); |
||
912 | $valuesCriteria = $this->buildCriteria(); |
||
913 | |||
914 | return $selectCriteria->doUpdate($valuesCriteria, $con); |
||
915 | } |
||
916 | |||
917 | /** |
||
918 | * Retrieves a field from the object by name passed in as a string. |
||
919 | * |
||
920 | * @param string $name name |
||
921 | * @param string $type The type of fieldname the $name is of: |
||
922 | * one of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_CAMELNAME |
||
923 | * TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM. |
||
924 | * Defaults to TableMap::TYPE_PHPNAME. |
||
925 | * @return mixed Value of field. |
||
926 | */ |
||
927 | public function getByName($name, $type = TableMap::TYPE_PHPNAME) |
||
928 | { |
||
929 | $pos = ConnectionTableMap::translateFieldName($name, $type, TableMap::TYPE_NUM); |
||
930 | $field = $this->getByPosition($pos); |
||
931 | |||
932 | return $field; |
||
933 | } |
||
934 | |||
935 | /** |
||
936 | * Retrieves a field from the object by Position as specified in the xml schema. |
||
937 | * Zero-based. |
||
938 | * |
||
939 | * @param int $pos position in xml schema |
||
940 | * @return mixed Value of field at $pos |
||
941 | */ |
||
942 | public function getByPosition($pos) |
||
943 | { |
||
944 | switch ($pos) { |
||
945 | case 0: |
||
946 | return $this->getId(); |
||
947 | break; |
||
948 | case 1: |
||
949 | return $this->getInstanceName(); |
||
950 | break; |
||
951 | case 2: |
||
952 | return $this->getUserId(); |
||
953 | break; |
||
954 | case 3: |
||
955 | return $this->getPeer(); |
||
956 | break; |
||
957 | case 4: |
||
958 | return $this->getStarted(); |
||
959 | break; |
||
960 | case 5: |
||
961 | return $this->getType(); |
||
962 | break; |
||
963 | default: |
||
964 | return null; |
||
965 | break; |
||
966 | } // switch() |
||
967 | } |
||
968 | |||
969 | /** |
||
970 | * Exports the object as an array. |
||
971 | * |
||
972 | * You can specify the key type of the array by passing one of the class |
||
973 | * type constants. |
||
974 | * |
||
975 | * @param string $keyType (optional) One of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_CAMELNAME, |
||
976 | * TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM. |
||
977 | * Defaults to TableMap::TYPE_PHPNAME. |
||
978 | * @param boolean $includeLazyLoadColumns (optional) Whether to include lazy loaded columns. Defaults to TRUE. |
||
979 | * @param array $alreadyDumpedObjects List of objects to skip to avoid recursion |
||
980 | * @param boolean $includeForeignObjects (optional) Whether to include hydrated related objects. Default to FALSE. |
||
981 | * |
||
982 | * @return array an associative array containing the field names (as keys) and field values |
||
983 | */ |
||
984 | public function toArray($keyType = TableMap::TYPE_PHPNAME, $includeLazyLoadColumns = true, $alreadyDumpedObjects = array(), $includeForeignObjects = false) |
||
985 | { |
||
986 | |||
987 | if (isset($alreadyDumpedObjects['Connection'][$this->hashCode()])) { |
||
988 | return '*RECURSION*'; |
||
989 | } |
||
990 | $alreadyDumpedObjects['Connection'][$this->hashCode()] = true; |
||
991 | $keys = ConnectionTableMap::getFieldNames($keyType); |
||
992 | $result = array( |
||
993 | $keys[0] => $this->getId(), |
||
994 | $keys[1] => $this->getInstanceName(), |
||
995 | $keys[2] => $this->getUserId(), |
||
996 | $keys[3] => $this->getPeer(), |
||
997 | $keys[4] => $this->getStarted(), |
||
998 | $keys[5] => $this->getType(), |
||
999 | ); |
||
1000 | if ($result[$keys[4]] instanceof \DateTime) { |
||
1001 | $result[$keys[4]] = $result[$keys[4]]->format('c'); |
||
1002 | } |
||
1003 | |||
1004 | $virtualColumns = $this->virtualColumns; |
||
1005 | foreach ($virtualColumns as $key => $virtualColumn) { |
||
1006 | $result[$key] = $virtualColumn; |
||
1007 | } |
||
1008 | |||
1009 | if ($includeForeignObjects) { |
||
1010 | if (null !== $this->aInstance) { |
||
1011 | |||
1012 | switch ($keyType) { |
||
1013 | case TableMap::TYPE_CAMELNAME: |
||
1014 | $key = 'instance'; |
||
1015 | break; |
||
1016 | case TableMap::TYPE_FIELDNAME: |
||
1017 | $key = 'instance'; |
||
1018 | break; |
||
1019 | default: |
||
1020 | $key = 'Instance'; |
||
1021 | } |
||
1022 | |||
1023 | $result[$key] = $this->aInstance->toArray($keyType, $includeLazyLoadColumns, $alreadyDumpedObjects, true); |
||
1024 | } |
||
1025 | if (null !== $this->aUser) { |
||
1026 | |||
1027 | switch ($keyType) { |
||
1028 | case TableMap::TYPE_CAMELNAME: |
||
1029 | $key = 'user'; |
||
1030 | break; |
||
1031 | case TableMap::TYPE_FIELDNAME: |
||
1032 | $key = 'user'; |
||
1033 | break; |
||
1034 | default: |
||
1035 | $key = 'User'; |
||
1036 | } |
||
1037 | |||
1038 | $result[$key] = $this->aUser->toArray($keyType, $includeLazyLoadColumns, $alreadyDumpedObjects, true); |
||
1039 | } |
||
1040 | } |
||
1041 | |||
1042 | return $result; |
||
1043 | } |
||
1044 | |||
1045 | /** |
||
1046 | * Sets a field from the object by name passed in as a string. |
||
1047 | * |
||
1048 | * @param string $name |
||
1049 | * @param mixed $value field value |
||
1050 | * @param string $type The type of fieldname the $name is of: |
||
1051 | * one of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_CAMELNAME |
||
1052 | * TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM. |
||
1053 | * Defaults to TableMap::TYPE_PHPNAME. |
||
1054 | * @return $this|\Jalle19\StatusManager\Database\Connection |
||
1055 | */ |
||
1056 | public function setByName($name, $value, $type = TableMap::TYPE_PHPNAME) |
||
1057 | { |
||
1058 | $pos = ConnectionTableMap::translateFieldName($name, $type, TableMap::TYPE_NUM); |
||
1059 | |||
1060 | return $this->setByPosition($pos, $value); |
||
1061 | } |
||
1062 | |||
1063 | /** |
||
1064 | * Sets a field from the object by Position as specified in the xml schema. |
||
1065 | * Zero-based. |
||
1066 | * |
||
1067 | * @param int $pos position in xml schema |
||
1068 | * @param mixed $value field value |
||
1069 | * @return $this|\Jalle19\StatusManager\Database\Connection |
||
1070 | */ |
||
1071 | public function setByPosition($pos, $value) |
||
1072 | { |
||
1073 | switch ($pos) { |
||
1074 | case 0: |
||
1075 | $this->setId($value); |
||
1076 | break; |
||
1077 | case 1: |
||
1078 | $this->setInstanceName($value); |
||
1079 | break; |
||
1080 | case 2: |
||
1081 | $this->setUserId($value); |
||
1082 | break; |
||
1083 | case 3: |
||
1084 | $this->setPeer($value); |
||
1085 | break; |
||
1086 | case 4: |
||
1087 | $this->setStarted($value); |
||
1088 | break; |
||
1089 | case 5: |
||
1090 | $this->setType($value); |
||
1091 | break; |
||
1092 | } // switch() |
||
1093 | |||
1094 | return $this; |
||
1095 | } |
||
1096 | |||
1097 | /** |
||
1098 | * Populates the object using an array. |
||
1099 | * |
||
1100 | * This is particularly useful when populating an object from one of the |
||
1101 | * request arrays (e.g. $_POST). This method goes through the column |
||
1102 | * names, checking to see whether a matching key exists in populated |
||
1103 | * array. If so the setByName() method is called for that column. |
||
1104 | * |
||
1105 | * You can specify the key type of the array by additionally passing one |
||
1106 | * of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_CAMELNAME, |
||
1107 | * TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM. |
||
1108 | * The default key type is the column's TableMap::TYPE_PHPNAME. |
||
1109 | * |
||
1110 | * @param array $arr An array to populate the object from. |
||
1111 | * @param string $keyType The type of keys the array uses. |
||
1112 | * @return void |
||
1113 | */ |
||
1114 | public function fromArray($arr, $keyType = TableMap::TYPE_PHPNAME) |
||
1115 | { |
||
1116 | $keys = ConnectionTableMap::getFieldNames($keyType); |
||
1117 | |||
1118 | if (array_key_exists($keys[0], $arr)) { |
||
1119 | $this->setId($arr[$keys[0]]); |
||
1120 | } |
||
1121 | if (array_key_exists($keys[1], $arr)) { |
||
1122 | $this->setInstanceName($arr[$keys[1]]); |
||
1123 | } |
||
1124 | if (array_key_exists($keys[2], $arr)) { |
||
1125 | $this->setUserId($arr[$keys[2]]); |
||
1126 | } |
||
1127 | if (array_key_exists($keys[3], $arr)) { |
||
1128 | $this->setPeer($arr[$keys[3]]); |
||
1129 | } |
||
1130 | if (array_key_exists($keys[4], $arr)) { |
||
1131 | $this->setStarted($arr[$keys[4]]); |
||
1132 | } |
||
1133 | if (array_key_exists($keys[5], $arr)) { |
||
1134 | $this->setType($arr[$keys[5]]); |
||
1135 | } |
||
1136 | } |
||
1137 | |||
1138 | /** |
||
1139 | * Populate the current object from a string, using a given parser format |
||
1140 | * <code> |
||
1141 | * $book = new Book(); |
||
1142 | * $book->importFrom('JSON', '{"Id":9012,"Title":"Don Juan","ISBN":"0140422161","Price":12.99,"PublisherId":1234,"AuthorId":5678}'); |
||
1143 | * </code> |
||
1144 | * |
||
1145 | * You can specify the key type of the array by additionally passing one |
||
1146 | * of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_CAMELNAME, |
||
1147 | * TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM. |
||
1148 | * The default key type is the column's TableMap::TYPE_PHPNAME. |
||
1149 | * |
||
1150 | * @param mixed $parser A AbstractParser instance, |
||
1151 | * or a format name ('XML', 'YAML', 'JSON', 'CSV') |
||
1152 | * @param string $data The source data to import from |
||
1153 | * @param string $keyType The type of keys the array uses. |
||
1154 | * |
||
1155 | * @return $this|\Jalle19\StatusManager\Database\Connection The current object, for fluid interface |
||
1156 | */ |
||
1157 | public function importFrom($parser, $data, $keyType = TableMap::TYPE_PHPNAME) |
||
1158 | { |
||
1159 | if (!$parser instanceof AbstractParser) { |
||
1160 | $parser = AbstractParser::getParser($parser); |
||
1161 | } |
||
1162 | |||
1163 | $this->fromArray($parser->toArray($data), $keyType); |
||
1164 | |||
1165 | return $this; |
||
1166 | } |
||
1167 | |||
1168 | /** |
||
1169 | * Build a Criteria object containing the values of all modified columns in this object. |
||
1170 | * |
||
1171 | * @return Criteria The Criteria object containing all modified values. |
||
1172 | */ |
||
1173 | public function buildCriteria() |
||
1174 | { |
||
1175 | $criteria = new Criteria(ConnectionTableMap::DATABASE_NAME); |
||
1176 | |||
1177 | if ($this->isColumnModified(ConnectionTableMap::COL_ID)) { |
||
1178 | $criteria->add(ConnectionTableMap::COL_ID, $this->id); |
||
1179 | } |
||
1180 | if ($this->isColumnModified(ConnectionTableMap::COL_INSTANCE_NAME)) { |
||
1181 | $criteria->add(ConnectionTableMap::COL_INSTANCE_NAME, $this->instance_name); |
||
1182 | } |
||
1183 | if ($this->isColumnModified(ConnectionTableMap::COL_USER_ID)) { |
||
1184 | $criteria->add(ConnectionTableMap::COL_USER_ID, $this->user_id); |
||
1185 | } |
||
1186 | if ($this->isColumnModified(ConnectionTableMap::COL_PEER)) { |
||
1187 | $criteria->add(ConnectionTableMap::COL_PEER, $this->peer); |
||
1188 | } |
||
1189 | if ($this->isColumnModified(ConnectionTableMap::COL_STARTED)) { |
||
1190 | $criteria->add(ConnectionTableMap::COL_STARTED, $this->started); |
||
1191 | } |
||
1192 | if ($this->isColumnModified(ConnectionTableMap::COL_TYPE)) { |
||
1193 | $criteria->add(ConnectionTableMap::COL_TYPE, $this->type); |
||
1194 | } |
||
1195 | |||
1196 | return $criteria; |
||
1197 | } |
||
1198 | |||
1199 | /** |
||
1200 | * Builds a Criteria object containing the primary key for this object. |
||
1201 | * |
||
1202 | * Unlike buildCriteria() this method includes the primary key values regardless |
||
1203 | * of whether or not they have been modified. |
||
1204 | * |
||
1205 | * @throws LogicException if no primary key is defined |
||
1206 | * |
||
1207 | * @return Criteria The Criteria object containing value(s) for primary key(s). |
||
1208 | */ |
||
1209 | public function buildPkeyCriteria() |
||
1210 | { |
||
1211 | $criteria = ChildConnectionQuery::create(); |
||
1212 | $criteria->add(ConnectionTableMap::COL_ID, $this->id); |
||
1213 | |||
1214 | return $criteria; |
||
1215 | } |
||
1216 | |||
1217 | /** |
||
1218 | * If the primary key is not null, return the hashcode of the |
||
1219 | * primary key. Otherwise, return the hash code of the object. |
||
1220 | * |
||
1221 | * @return int Hashcode |
||
1222 | */ |
||
1223 | public function hashCode() |
||
1224 | { |
||
1225 | $validPk = null !== $this->getId(); |
||
1226 | |||
1227 | $validPrimaryKeyFKs = 0; |
||
1228 | $primaryKeyFKs = []; |
||
1229 | |||
1230 | if ($validPk) { |
||
1231 | return crc32(json_encode($this->getPrimaryKey(), JSON_UNESCAPED_UNICODE)); |
||
1232 | } elseif ($validPrimaryKeyFKs) { |
||
1233 | return crc32(json_encode($primaryKeyFKs, JSON_UNESCAPED_UNICODE)); |
||
1234 | } |
||
1235 | |||
1236 | return spl_object_hash($this); |
||
1237 | } |
||
1238 | |||
1239 | /** |
||
1240 | * Returns the primary key for this object (row). |
||
1241 | * @return int |
||
1242 | */ |
||
1243 | public function getPrimaryKey() |
||
1244 | { |
||
1245 | return $this->getId(); |
||
1246 | } |
||
1247 | |||
1248 | /** |
||
1249 | * Generic method to set the primary key (id column). |
||
1250 | * |
||
1251 | * @param int $key Primary key. |
||
1252 | * @return void |
||
1253 | */ |
||
1254 | public function setPrimaryKey($key) |
||
1255 | { |
||
1256 | $this->setId($key); |
||
1257 | } |
||
1258 | |||
1259 | /** |
||
1260 | * Returns true if the primary key for this object is null. |
||
1261 | * @return boolean |
||
1262 | */ |
||
1263 | public function isPrimaryKeyNull() |
||
1264 | { |
||
1265 | return null === $this->getId(); |
||
1266 | } |
||
1267 | |||
1268 | /** |
||
1269 | * Sets contents of passed object to values from current object. |
||
1270 | * |
||
1271 | * If desired, this method can also make copies of all associated (fkey referrers) |
||
1272 | * objects. |
||
1273 | * |
||
1274 | * @param object $copyObj An object of \Jalle19\StatusManager\Database\Connection (or compatible) type. |
||
1275 | * @param boolean $deepCopy Whether to also copy all rows that refer (by fkey) to the current row. |
||
1276 | * @param boolean $makeNew Whether to reset autoincrement PKs and make the object new. |
||
1277 | * @throws PropelException |
||
1278 | */ |
||
1279 | public function copyInto($copyObj, $deepCopy = false, $makeNew = true) |
||
1280 | { |
||
1281 | $copyObj->setInstanceName($this->getInstanceName()); |
||
1282 | $copyObj->setUserId($this->getUserId()); |
||
1283 | $copyObj->setPeer($this->getPeer()); |
||
1284 | $copyObj->setStarted($this->getStarted()); |
||
1285 | $copyObj->setType($this->getType()); |
||
1286 | if ($makeNew) { |
||
1287 | $copyObj->setNew(true); |
||
1288 | $copyObj->setId(NULL); // this is a auto-increment column, so set to default value |
||
1289 | } |
||
1290 | } |
||
1291 | |||
1292 | /** |
||
1293 | * Makes a copy of this object that will be inserted as a new row in table when saved. |
||
1294 | * It creates a new object filling in the simple attributes, but skipping any primary |
||
1295 | * keys that are defined for the table. |
||
1296 | * |
||
1297 | * If desired, this method can also make copies of all associated (fkey referrers) |
||
1298 | * objects. |
||
1299 | * |
||
1300 | * @param boolean $deepCopy Whether to also copy all rows that refer (by fkey) to the current row. |
||
1301 | * @return \Jalle19\StatusManager\Database\Connection Clone of current object. |
||
1302 | * @throws PropelException |
||
1303 | */ |
||
1304 | public function copy($deepCopy = false) |
||
1305 | { |
||
1306 | // we use get_class(), because this might be a subclass |
||
1307 | $clazz = get_class($this); |
||
1308 | $copyObj = new $clazz(); |
||
1309 | $this->copyInto($copyObj, $deepCopy); |
||
1310 | |||
1311 | return $copyObj; |
||
1312 | } |
||
1313 | |||
1314 | /** |
||
1315 | * Declares an association between this object and a ChildInstance object. |
||
1316 | * |
||
1317 | * @param ChildInstance $v |
||
1318 | * @return $this|\Jalle19\StatusManager\Database\Connection The current object (for fluent API support) |
||
1319 | * @throws PropelException |
||
1320 | */ |
||
1321 | public function setInstance(ChildInstance $v = null) |
||
1322 | { |
||
1323 | if ($v === null) { |
||
1324 | $this->setInstanceName(NULL); |
||
1325 | } else { |
||
1326 | $this->setInstanceName($v->getName()); |
||
1327 | } |
||
1328 | |||
1329 | $this->aInstance = $v; |
||
1330 | |||
1331 | // Add binding for other direction of this n:n relationship. |
||
1332 | // If this object has already been added to the ChildInstance object, it will not be re-added. |
||
1333 | if ($v !== null) { |
||
1334 | $v->addConnection($this); |
||
1335 | } |
||
1336 | |||
1337 | |||
1338 | return $this; |
||
1339 | } |
||
1340 | |||
1341 | |||
1342 | /** |
||
1343 | * Get the associated ChildInstance object |
||
1344 | * |
||
1345 | * @param ConnectionInterface $con Optional Connection object. |
||
1346 | * @return ChildInstance The associated ChildInstance object. |
||
1347 | * @throws PropelException |
||
1348 | */ |
||
1349 | public function getInstance(ConnectionInterface $con = null) |
||
1350 | { |
||
1351 | if ($this->aInstance === null && (($this->instance_name !== "" && $this->instance_name !== null))) { |
||
1352 | $this->aInstance = ChildInstanceQuery::create()->findPk($this->instance_name, $con); |
||
1353 | /* The following can be used additionally to |
||
1354 | guarantee the related object contains a reference |
||
1355 | to this object. This level of coupling may, however, be |
||
1356 | undesirable since it could result in an only partially populated collection |
||
1357 | in the referenced object. |
||
1358 | $this->aInstance->addConnections($this); |
||
1359 | */ |
||
1360 | } |
||
1361 | |||
1362 | return $this->aInstance; |
||
1363 | } |
||
1364 | |||
1365 | /** |
||
1366 | * Declares an association between this object and a ChildUser object. |
||
1367 | * |
||
1368 | * @param ChildUser $v |
||
1369 | * @return $this|\Jalle19\StatusManager\Database\Connection The current object (for fluent API support) |
||
1370 | * @throws PropelException |
||
1371 | */ |
||
1372 | public function setUser(ChildUser $v = null) |
||
1373 | { |
||
1374 | if ($v === null) { |
||
1375 | $this->setUserId(NULL); |
||
1376 | } else { |
||
1377 | $this->setUserId($v->getId()); |
||
1378 | } |
||
1379 | |||
1380 | $this->aUser = $v; |
||
1381 | |||
1382 | // Add binding for other direction of this n:n relationship. |
||
1383 | // If this object has already been added to the ChildUser object, it will not be re-added. |
||
1384 | if ($v !== null) { |
||
1385 | $v->addConnection($this); |
||
1386 | } |
||
1387 | |||
1388 | |||
1389 | return $this; |
||
1390 | } |
||
1391 | |||
1392 | |||
1393 | /** |
||
1394 | * Get the associated ChildUser object |
||
1395 | * |
||
1396 | * @param ConnectionInterface $con Optional Connection object. |
||
1397 | * @return ChildUser The associated ChildUser object. |
||
1398 | * @throws PropelException |
||
1399 | */ |
||
1400 | public function getUser(ConnectionInterface $con = null) |
||
1401 | { |
||
1402 | if ($this->aUser === null && ($this->user_id !== null)) { |
||
1403 | $this->aUser = ChildUserQuery::create()->findPk($this->user_id, $con); |
||
1404 | /* The following can be used additionally to |
||
1405 | guarantee the related object contains a reference |
||
1406 | to this object. This level of coupling may, however, be |
||
1407 | undesirable since it could result in an only partially populated collection |
||
1408 | in the referenced object. |
||
1409 | $this->aUser->addConnections($this); |
||
1410 | */ |
||
1411 | } |
||
1412 | |||
1413 | return $this->aUser; |
||
1414 | } |
||
1415 | |||
1416 | /** |
||
1417 | * Clears the current object, sets all attributes to their default values and removes |
||
1418 | * outgoing references as well as back-references (from other objects to this one. Results probably in a database |
||
1419 | * change of those foreign objects when you call `save` there). |
||
1420 | */ |
||
1421 | public function clear() |
||
1422 | { |
||
1423 | if (null !== $this->aInstance) { |
||
1424 | $this->aInstance->removeConnection($this); |
||
1425 | } |
||
1426 | if (null !== $this->aUser) { |
||
1427 | $this->aUser->removeConnection($this); |
||
1428 | } |
||
1429 | $this->id = null; |
||
1430 | $this->instance_name = null; |
||
1431 | $this->user_id = null; |
||
1432 | $this->peer = null; |
||
1433 | $this->started = null; |
||
1434 | $this->type = null; |
||
1435 | $this->alreadyInSave = false; |
||
1436 | $this->clearAllReferences(); |
||
1437 | $this->resetModified(); |
||
1438 | $this->setNew(true); |
||
1439 | $this->setDeleted(false); |
||
1440 | } |
||
1441 | |||
1442 | /** |
||
1443 | * Resets all references and back-references to other model objects or collections of model objects. |
||
1444 | * |
||
1445 | * This method is used to reset all php object references (not the actual reference in the database). |
||
1446 | * Necessary for object serialisation. |
||
1447 | * |
||
1448 | * @param boolean $deep Whether to also clear the references on all referrer objects. |
||
1449 | */ |
||
1450 | public function clearAllReferences($deep = false) |
||
1451 | { |
||
1452 | if ($deep) { |
||
1453 | } // if ($deep) |
||
1454 | |||
1455 | $this->aInstance = null; |
||
1456 | $this->aUser = null; |
||
1457 | } |
||
1458 | |||
1459 | /** |
||
1460 | * Return the string representation of this object |
||
1461 | * |
||
1462 | * @return string |
||
1463 | */ |
||
1464 | public function __toString() |
||
1465 | { |
||
1466 | return (string) $this->exportTo(ConnectionTableMap::DEFAULT_STRING_FORMAT); |
||
1467 | } |
||
1468 | |||
1469 | /** |
||
1470 | * Code to be run before persisting the object |
||
1471 | * @param ConnectionInterface $con |
||
1472 | * @return boolean |
||
1473 | */ |
||
1474 | public function preSave(ConnectionInterface $con = null) |
||
1475 | { |
||
1476 | return true; |
||
1477 | } |
||
1478 | |||
1479 | /** |
||
1480 | * Code to be run after persisting the object |
||
1481 | * @param ConnectionInterface $con |
||
1482 | */ |
||
1483 | public function postSave(ConnectionInterface $con = null) |
||
1484 | { |
||
1485 | |||
1486 | } |
||
1487 | |||
1488 | /** |
||
1489 | * Code to be run before inserting to database |
||
1490 | * @param ConnectionInterface $con |
||
1491 | * @return boolean |
||
1492 | */ |
||
1493 | public function preInsert(ConnectionInterface $con = null) |
||
1494 | { |
||
1495 | return true; |
||
1496 | } |
||
1497 | |||
1498 | /** |
||
1499 | * Code to be run after inserting to database |
||
1500 | * @param ConnectionInterface $con |
||
1501 | */ |
||
1502 | public function postInsert(ConnectionInterface $con = null) |
||
1503 | { |
||
1504 | |||
1505 | } |
||
1506 | |||
1507 | /** |
||
1508 | * Code to be run before updating the object in database |
||
1509 | * @param ConnectionInterface $con |
||
1510 | * @return boolean |
||
1511 | */ |
||
1512 | public function preUpdate(ConnectionInterface $con = null) |
||
1513 | { |
||
1514 | return true; |
||
1515 | } |
||
1516 | |||
1517 | /** |
||
1518 | * Code to be run after updating the object in database |
||
1519 | * @param ConnectionInterface $con |
||
1520 | */ |
||
1521 | public function postUpdate(ConnectionInterface $con = null) |
||
1522 | { |
||
1523 | |||
1524 | } |
||
1525 | |||
1526 | /** |
||
1527 | * Code to be run before deleting the object in database |
||
1528 | * @param ConnectionInterface $con |
||
1529 | * @return boolean |
||
1530 | */ |
||
1531 | public function preDelete(ConnectionInterface $con = null) |
||
1532 | { |
||
1533 | return true; |
||
1534 | } |
||
1535 | |||
1536 | /** |
||
1537 | * Code to be run after deleting the object in database |
||
1538 | * @param ConnectionInterface $con |
||
1539 | */ |
||
1540 | public function postDelete(ConnectionInterface $con = null) |
||
1541 | { |
||
1542 | |||
1543 | } |
||
1544 | |||
1545 | |||
1546 | /** |
||
1547 | * Derived method to catches calls to undefined methods. |
||
1548 | * |
||
1549 | * Provides magic import/export method support (fromXML()/toXML(), fromYAML()/toYAML(), etc.). |
||
1550 | * Allows to define default __call() behavior if you overwrite __call() |
||
1551 | * |
||
1552 | * @param string $name |
||
1553 | * @param mixed $params |
||
1554 | * |
||
1555 | * @return array|string |
||
1556 | */ |
||
1557 | public function __call($name, $params) |
||
1558 | { |
||
1559 | if (0 === strpos($name, 'get')) { |
||
1560 | $virtualColumn = substr($name, 3); |
||
1561 | if ($this->hasVirtualColumn($virtualColumn)) { |
||
1562 | return $this->getVirtualColumn($virtualColumn); |
||
1563 | } |
||
1564 | |||
1565 | $virtualColumn = lcfirst($virtualColumn); |
||
1566 | if ($this->hasVirtualColumn($virtualColumn)) { |
||
1567 | return $this->getVirtualColumn($virtualColumn); |
||
1568 | } |
||
1569 | } |
||
1570 | |||
1571 | if (0 === strpos($name, 'from')) { |
||
1572 | $format = substr($name, 4); |
||
1573 | |||
1574 | return $this->importFrom($format, reset($params)); |
||
1575 | } |
||
1576 | |||
1577 | if (0 === strpos($name, 'to')) { |
||
1578 | $format = substr($name, 2); |
||
1579 | $includeLazyLoadColumns = isset($params[0]) ? $params[0] : true; |
||
1580 | |||
1581 | return $this->exportTo($format, $includeLazyLoadColumns); |
||
1582 | } |
||
1583 | |||
1584 | throw new BadMethodCallException(sprintf('Call to undefined method: %s.', $name)); |
||
1585 | } |
||
1586 | |||
1587 | } |
||
1588 |