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 Instance 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 Instance, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
42 | abstract class Instance implements ActiveRecordInterface |
||
43 | { |
||
44 | /** |
||
45 | * TableMap class name |
||
46 | */ |
||
47 | const TABLE_MAP = '\\Jalle19\\StatusManager\\Database\\Map\\InstanceTableMap'; |
||
48 | |||
49 | |||
50 | /** |
||
51 | * attribute to determine if this object has previously been saved. |
||
52 | * @var boolean |
||
53 | */ |
||
54 | protected $new = true; |
||
55 | |||
56 | /** |
||
57 | * attribute to determine whether this object has been deleted. |
||
58 | * @var boolean |
||
59 | */ |
||
60 | protected $deleted = false; |
||
61 | |||
62 | /** |
||
63 | * The columns that have been modified in current object. |
||
64 | * Tracking modified columns allows us to only update modified columns. |
||
65 | * @var array |
||
66 | */ |
||
67 | protected $modifiedColumns = array(); |
||
68 | |||
69 | /** |
||
70 | * The (virtual) columns that are added at runtime |
||
71 | * The formatters can add supplementary columns based on a resultset |
||
72 | * @var array |
||
73 | */ |
||
74 | protected $virtualColumns = array(); |
||
75 | |||
76 | /** |
||
77 | * The value for the name field. |
||
78 | * |
||
79 | * @var string |
||
80 | */ |
||
81 | protected $name; |
||
82 | |||
83 | /** |
||
84 | * @var ObjectCollection|ChildUser[] Collection to store aggregation of ChildUser objects. |
||
85 | */ |
||
86 | protected $collUsers; |
||
87 | protected $collUsersPartial; |
||
88 | |||
89 | /** |
||
90 | * @var ObjectCollection|ChildConnection[] Collection to store aggregation of ChildConnection objects. |
||
91 | */ |
||
92 | protected $collConnections; |
||
93 | protected $collConnectionsPartial; |
||
94 | |||
95 | /** |
||
96 | * @var ObjectCollection|ChildChannel[] Collection to store aggregation of ChildChannel objects. |
||
97 | */ |
||
98 | protected $collChannels; |
||
99 | protected $collChannelsPartial; |
||
100 | |||
101 | /** |
||
102 | * @var ObjectCollection|ChildSubscription[] Collection to store aggregation of ChildSubscription objects. |
||
103 | */ |
||
104 | protected $collSubscriptions; |
||
105 | protected $collSubscriptionsPartial; |
||
106 | |||
107 | /** |
||
108 | * Flag to prevent endless save loop, if this object is referenced |
||
109 | * by another object which falls in this transaction. |
||
110 | * |
||
111 | * @var boolean |
||
112 | */ |
||
113 | protected $alreadyInSave = false; |
||
114 | |||
115 | /** |
||
116 | * An array of objects scheduled for deletion. |
||
117 | * @var ObjectCollection|ChildUser[] |
||
118 | */ |
||
119 | protected $usersScheduledForDeletion = null; |
||
120 | |||
121 | /** |
||
122 | * An array of objects scheduled for deletion. |
||
123 | * @var ObjectCollection|ChildConnection[] |
||
124 | */ |
||
125 | protected $connectionsScheduledForDeletion = null; |
||
126 | |||
127 | /** |
||
128 | * An array of objects scheduled for deletion. |
||
129 | * @var ObjectCollection|ChildChannel[] |
||
130 | */ |
||
131 | protected $channelsScheduledForDeletion = null; |
||
132 | |||
133 | /** |
||
134 | * An array of objects scheduled for deletion. |
||
135 | * @var ObjectCollection|ChildSubscription[] |
||
136 | */ |
||
137 | protected $subscriptionsScheduledForDeletion = null; |
||
138 | |||
139 | /** |
||
140 | * Initializes internal state of Jalle19\StatusManager\Database\Base\Instance object. |
||
141 | */ |
||
142 | public function __construct() |
||
143 | { |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Returns whether the object has been modified. |
||
148 | * |
||
149 | * @return boolean True if the object has been modified. |
||
150 | */ |
||
151 | public function isModified() |
||
152 | { |
||
153 | return !!$this->modifiedColumns; |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Has specified column been modified? |
||
158 | * |
||
159 | * @param string $col column fully qualified name (TableMap::TYPE_COLNAME), e.g. Book::AUTHOR_ID |
||
160 | * @return boolean True if $col has been modified. |
||
161 | */ |
||
162 | public function isColumnModified($col) |
||
163 | { |
||
164 | return $this->modifiedColumns && isset($this->modifiedColumns[$col]); |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Get the columns that have been modified in this object. |
||
169 | * @return array A unique list of the modified column names for this object. |
||
170 | */ |
||
171 | public function getModifiedColumns() |
||
172 | { |
||
173 | return $this->modifiedColumns ? array_keys($this->modifiedColumns) : []; |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Returns whether the object has ever been saved. This will |
||
178 | * be false, if the object was retrieved from storage or was created |
||
179 | * and then saved. |
||
180 | * |
||
181 | * @return boolean true, if the object has never been persisted. |
||
182 | */ |
||
183 | public function isNew() |
||
184 | { |
||
185 | return $this->new; |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Setter for the isNew attribute. This method will be called |
||
190 | * by Propel-generated children and objects. |
||
191 | * |
||
192 | * @param boolean $b the state of the object. |
||
193 | */ |
||
194 | public function setNew($b) |
||
195 | { |
||
196 | $this->new = (boolean) $b; |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * Whether this object has been deleted. |
||
201 | * @return boolean The deleted state of this object. |
||
202 | */ |
||
203 | public function isDeleted() |
||
204 | { |
||
205 | return $this->deleted; |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * Specify whether this object has been deleted. |
||
210 | * @param boolean $b The deleted state of this object. |
||
211 | * @return void |
||
212 | */ |
||
213 | public function setDeleted($b) |
||
214 | { |
||
215 | $this->deleted = (boolean) $b; |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * Sets the modified state for the object to be false. |
||
220 | * @param string $col If supplied, only the specified column is reset. |
||
221 | * @return void |
||
222 | */ |
||
223 | public function resetModified($col = null) |
||
224 | { |
||
225 | if (null !== $col) { |
||
226 | if (isset($this->modifiedColumns[$col])) { |
||
227 | unset($this->modifiedColumns[$col]); |
||
228 | } |
||
229 | } else { |
||
230 | $this->modifiedColumns = array(); |
||
231 | } |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * Compares this with another <code>Instance</code> instance. If |
||
236 | * <code>obj</code> is an instance of <code>Instance</code>, delegates to |
||
237 | * <code>equals(Instance)</code>. Otherwise, returns <code>false</code>. |
||
238 | * |
||
239 | * @param mixed $obj The object to compare to. |
||
240 | * @return boolean Whether equal to the object specified. |
||
241 | */ |
||
242 | public function equals($obj) |
||
243 | { |
||
244 | if (!$obj instanceof static) { |
||
245 | return false; |
||
246 | } |
||
247 | |||
248 | if ($this === $obj) { |
||
249 | return true; |
||
250 | } |
||
251 | |||
252 | if (null === $this->getPrimaryKey() || null === $obj->getPrimaryKey()) { |
||
253 | return false; |
||
254 | } |
||
255 | |||
256 | return $this->getPrimaryKey() === $obj->getPrimaryKey(); |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * Get the associative array of the virtual columns in this object |
||
261 | * |
||
262 | * @return array |
||
263 | */ |
||
264 | public function getVirtualColumns() |
||
265 | { |
||
266 | return $this->virtualColumns; |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * Checks the existence of a virtual column in this object |
||
271 | * |
||
272 | * @param string $name The virtual column name |
||
273 | * @return boolean |
||
274 | */ |
||
275 | public function hasVirtualColumn($name) |
||
276 | { |
||
277 | return array_key_exists($name, $this->virtualColumns); |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * Get the value of a virtual column in this object |
||
282 | * |
||
283 | * @param string $name The virtual column name |
||
284 | * @return mixed |
||
285 | * |
||
286 | * @throws PropelException |
||
287 | */ |
||
288 | public function getVirtualColumn($name) |
||
289 | { |
||
290 | if (!$this->hasVirtualColumn($name)) { |
||
291 | throw new PropelException(sprintf('Cannot get value of inexistent virtual column %s.', $name)); |
||
292 | } |
||
293 | |||
294 | return $this->virtualColumns[$name]; |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * Set the value of a virtual column in this object |
||
299 | * |
||
300 | * @param string $name The virtual column name |
||
301 | * @param mixed $value The value to give to the virtual column |
||
302 | * |
||
303 | * @return $this|Instance The current object, for fluid interface |
||
304 | */ |
||
305 | public function setVirtualColumn($name, $value) |
||
306 | { |
||
307 | $this->virtualColumns[$name] = $value; |
||
308 | |||
309 | return $this; |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * Logs a message using Propel::log(). |
||
314 | * |
||
315 | * @param string $msg |
||
316 | * @param int $priority One of the Propel::LOG_* logging levels |
||
317 | * @return boolean |
||
318 | */ |
||
319 | protected function log($msg, $priority = Propel::LOG_INFO) |
||
320 | { |
||
321 | return Propel::log(get_class($this) . ': ' . $msg, $priority); |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * Export the current object properties to a string, using a given parser format |
||
326 | * <code> |
||
327 | * $book = BookQuery::create()->findPk(9012); |
||
328 | * echo $book->exportTo('JSON'); |
||
329 | * => {"Id":9012,"Title":"Don Juan","ISBN":"0140422161","Price":12.99,"PublisherId":1234,"AuthorId":5678}'); |
||
330 | * </code> |
||
331 | * |
||
332 | * @param mixed $parser A AbstractParser instance, or a format name ('XML', 'YAML', 'JSON', 'CSV') |
||
333 | * @param boolean $includeLazyLoadColumns (optional) Whether to include lazy load(ed) columns. Defaults to TRUE. |
||
334 | * @return string The exported data |
||
335 | */ |
||
336 | public function exportTo($parser, $includeLazyLoadColumns = true) |
||
337 | { |
||
338 | if (!$parser instanceof AbstractParser) { |
||
339 | $parser = AbstractParser::getParser($parser); |
||
340 | } |
||
341 | |||
342 | return $parser->fromArray($this->toArray(TableMap::TYPE_PHPNAME, $includeLazyLoadColumns, array(), true)); |
||
343 | } |
||
344 | |||
345 | /** |
||
346 | * Clean up internal collections prior to serializing |
||
347 | * Avoids recursive loops that turn into segmentation faults when serializing |
||
348 | */ |
||
349 | public function __sleep() |
||
350 | { |
||
351 | $this->clearAllReferences(); |
||
352 | |||
353 | $cls = new \ReflectionClass($this); |
||
354 | $propertyNames = []; |
||
355 | $serializableProperties = array_diff($cls->getProperties(), $cls->getProperties(\ReflectionProperty::IS_STATIC)); |
||
356 | |||
357 | foreach($serializableProperties as $property) { |
||
358 | $propertyNames[] = $property->getName(); |
||
359 | } |
||
360 | |||
361 | return $propertyNames; |
||
362 | } |
||
363 | |||
364 | /** |
||
365 | * Get the [name] column value. |
||
366 | * |
||
367 | * @return string |
||
368 | */ |
||
369 | public function getName() |
||
370 | { |
||
371 | return $this->name; |
||
372 | } |
||
373 | |||
374 | /** |
||
375 | * Set the value of [name] column. |
||
376 | * |
||
377 | * @param string $v new value |
||
378 | * @return $this|\Jalle19\StatusManager\Database\Instance The current object (for fluent API support) |
||
379 | */ |
||
380 | public function setName($v) |
||
381 | { |
||
382 | if ($v !== null) { |
||
383 | $v = (string) $v; |
||
384 | } |
||
385 | |||
386 | if ($this->name !== $v) { |
||
387 | $this->name = $v; |
||
388 | $this->modifiedColumns[InstanceTableMap::COL_NAME] = true; |
||
389 | } |
||
390 | |||
391 | return $this; |
||
392 | } // setName() |
||
393 | |||
394 | /** |
||
395 | * Indicates whether the columns in this object are only set to default values. |
||
396 | * |
||
397 | * This method can be used in conjunction with isModified() to indicate whether an object is both |
||
398 | * modified _and_ has some values set which are non-default. |
||
399 | * |
||
400 | * @return boolean Whether the columns in this object are only been set with default values. |
||
401 | */ |
||
402 | public function hasOnlyDefaultValues() |
||
403 | { |
||
404 | // otherwise, everything was equal, so return TRUE |
||
405 | return true; |
||
406 | } // hasOnlyDefaultValues() |
||
407 | |||
408 | /** |
||
409 | * Hydrates (populates) the object variables with values from the database resultset. |
||
410 | * |
||
411 | * An offset (0-based "start column") is specified so that objects can be hydrated |
||
412 | * with a subset of the columns in the resultset rows. This is needed, for example, |
||
413 | * for results of JOIN queries where the resultset row includes columns from two or |
||
414 | * more tables. |
||
415 | * |
||
416 | * @param array $row The row returned by DataFetcher->fetch(). |
||
417 | * @param int $startcol 0-based offset column which indicates which restultset column to start with. |
||
418 | * @param boolean $rehydrate Whether this object is being re-hydrated from the database. |
||
419 | * @param string $indexType The index type of $row. Mostly DataFetcher->getIndexType(). |
||
420 | One of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_CAMELNAME |
||
421 | * TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM. |
||
422 | * |
||
423 | * @return int next starting column |
||
424 | * @throws PropelException - Any caught Exception will be rewrapped as a PropelException. |
||
425 | */ |
||
426 | public function hydrate($row, $startcol = 0, $rehydrate = false, $indexType = TableMap::TYPE_NUM) |
||
427 | { |
||
428 | try { |
||
429 | |||
430 | $col = $row[TableMap::TYPE_NUM == $indexType ? 0 + $startcol : InstanceTableMap::translateFieldName('Name', TableMap::TYPE_PHPNAME, $indexType)]; |
||
431 | $this->name = (null !== $col) ? (string) $col : null; |
||
432 | $this->resetModified(); |
||
433 | |||
434 | $this->setNew(false); |
||
435 | |||
436 | if ($rehydrate) { |
||
437 | $this->ensureConsistency(); |
||
438 | } |
||
439 | |||
440 | return $startcol + 1; // 1 = InstanceTableMap::NUM_HYDRATE_COLUMNS. |
||
441 | |||
442 | } catch (Exception $e) { |
||
443 | throw new PropelException(sprintf('Error populating %s object', '\\Jalle19\\StatusManager\\Database\\Instance'), 0, $e); |
||
444 | } |
||
445 | } |
||
446 | |||
447 | /** |
||
448 | * Checks and repairs the internal consistency of the object. |
||
449 | * |
||
450 | * This method is executed after an already-instantiated object is re-hydrated |
||
451 | * from the database. It exists to check any foreign keys to make sure that |
||
452 | * the objects related to the current object are correct based on foreign key. |
||
453 | * |
||
454 | * You can override this method in the stub class, but you should always invoke |
||
455 | * the base method from the overridden method (i.e. parent::ensureConsistency()), |
||
456 | * in case your model changes. |
||
457 | * |
||
458 | * @throws PropelException |
||
459 | */ |
||
460 | public function ensureConsistency() |
||
461 | { |
||
462 | } // ensureConsistency |
||
463 | |||
464 | /** |
||
465 | * Reloads this object from datastore based on primary key and (optionally) resets all associated objects. |
||
466 | * |
||
467 | * This will only work if the object has been saved and has a valid primary key set. |
||
468 | * |
||
469 | * @param boolean $deep (optional) Whether to also de-associated any related objects. |
||
470 | * @param ConnectionInterface $con (optional) The ConnectionInterface connection to use. |
||
471 | * @return void |
||
472 | * @throws PropelException - if this object is deleted, unsaved or doesn't have pk match in db |
||
473 | */ |
||
474 | public function reload($deep = false, ConnectionInterface $con = null) |
||
475 | { |
||
476 | if ($this->isDeleted()) { |
||
477 | throw new PropelException("Cannot reload a deleted object."); |
||
478 | } |
||
479 | |||
480 | if ($this->isNew()) { |
||
481 | throw new PropelException("Cannot reload an unsaved object."); |
||
482 | } |
||
483 | |||
484 | if ($con === null) { |
||
485 | $con = Propel::getServiceContainer()->getReadConnection(InstanceTableMap::DATABASE_NAME); |
||
486 | } |
||
487 | |||
488 | // We don't need to alter the object instance pool; we're just modifying this instance |
||
489 | // already in the pool. |
||
490 | |||
491 | $dataFetcher = ChildInstanceQuery::create(null, $this->buildPkeyCriteria())->setFormatter(ModelCriteria::FORMAT_STATEMENT)->find($con); |
||
492 | $row = $dataFetcher->fetch(); |
||
493 | $dataFetcher->close(); |
||
494 | if (!$row) { |
||
495 | throw new PropelException('Cannot find matching row in the database to reload object values.'); |
||
496 | } |
||
497 | $this->hydrate($row, 0, true, $dataFetcher->getIndexType()); // rehydrate |
||
498 | |||
499 | if ($deep) { // also de-associate any related objects? |
||
500 | |||
501 | $this->collUsers = null; |
||
502 | |||
503 | $this->collConnections = null; |
||
504 | |||
505 | $this->collChannels = null; |
||
506 | |||
507 | $this->collSubscriptions = null; |
||
508 | |||
509 | } // if (deep) |
||
510 | } |
||
511 | |||
512 | /** |
||
513 | * Removes this object from datastore and sets delete attribute. |
||
514 | * |
||
515 | * @param ConnectionInterface $con |
||
516 | * @return void |
||
517 | * @throws PropelException |
||
518 | * @see Instance::setDeleted() |
||
519 | * @see Instance::isDeleted() |
||
520 | */ |
||
521 | public function delete(ConnectionInterface $con = null) |
||
522 | { |
||
523 | if ($this->isDeleted()) { |
||
524 | throw new PropelException("This object has already been deleted."); |
||
525 | } |
||
526 | |||
527 | if ($con === null) { |
||
528 | $con = Propel::getServiceContainer()->getWriteConnection(InstanceTableMap::DATABASE_NAME); |
||
529 | } |
||
530 | |||
531 | $con->transaction(function () use ($con) { |
||
532 | $deleteQuery = ChildInstanceQuery::create() |
||
533 | ->filterByPrimaryKey($this->getPrimaryKey()); |
||
534 | $ret = $this->preDelete($con); |
||
535 | if ($ret) { |
||
536 | $deleteQuery->delete($con); |
||
537 | $this->postDelete($con); |
||
538 | $this->setDeleted(true); |
||
539 | } |
||
540 | }); |
||
541 | } |
||
542 | |||
543 | /** |
||
544 | * Persists this object to the database. |
||
545 | * |
||
546 | * If the object is new, it inserts it; otherwise an update is performed. |
||
547 | * All modified related objects will also be persisted in the doSave() |
||
548 | * method. This method wraps all precipitate database operations in a |
||
549 | * single transaction. |
||
550 | * |
||
551 | * @param ConnectionInterface $con |
||
552 | * @return int The number of rows affected by this insert/update and any referring fk objects' save() operations. |
||
553 | * @throws PropelException |
||
554 | * @see doSave() |
||
555 | */ |
||
556 | public function save(ConnectionInterface $con = null) |
||
557 | { |
||
558 | if ($this->isDeleted()) { |
||
559 | throw new PropelException("You cannot save an object that has been deleted."); |
||
560 | } |
||
561 | |||
562 | if ($con === null) { |
||
563 | $con = Propel::getServiceContainer()->getWriteConnection(InstanceTableMap::DATABASE_NAME); |
||
564 | } |
||
565 | |||
566 | return $con->transaction(function () use ($con) { |
||
567 | $isInsert = $this->isNew(); |
||
568 | $ret = $this->preSave($con); |
||
569 | if ($isInsert) { |
||
570 | $ret = $ret && $this->preInsert($con); |
||
571 | } else { |
||
572 | $ret = $ret && $this->preUpdate($con); |
||
573 | } |
||
574 | if ($ret) { |
||
575 | $affectedRows = $this->doSave($con); |
||
576 | if ($isInsert) { |
||
577 | $this->postInsert($con); |
||
578 | } else { |
||
579 | $this->postUpdate($con); |
||
580 | } |
||
581 | $this->postSave($con); |
||
582 | InstanceTableMap::addInstanceToPool($this); |
||
583 | } else { |
||
584 | $affectedRows = 0; |
||
585 | } |
||
586 | |||
587 | return $affectedRows; |
||
588 | }); |
||
589 | } |
||
590 | |||
591 | /** |
||
592 | * Performs the work of inserting or updating the row in the database. |
||
593 | * |
||
594 | * If the object is new, it inserts it; otherwise an update is performed. |
||
595 | * All related objects are also updated in this method. |
||
596 | * |
||
597 | * @param ConnectionInterface $con |
||
598 | * @return int The number of rows affected by this insert/update and any referring fk objects' save() operations. |
||
599 | * @throws PropelException |
||
600 | * @see save() |
||
601 | */ |
||
602 | protected function doSave(ConnectionInterface $con) |
||
603 | { |
||
604 | $affectedRows = 0; // initialize var to track total num of affected rows |
||
605 | if (!$this->alreadyInSave) { |
||
606 | $this->alreadyInSave = true; |
||
607 | |||
608 | if ($this->isNew() || $this->isModified()) { |
||
609 | // persist changes |
||
610 | if ($this->isNew()) { |
||
611 | $this->doInsert($con); |
||
612 | $affectedRows += 1; |
||
613 | } else { |
||
614 | $affectedRows += $this->doUpdate($con); |
||
615 | } |
||
616 | $this->resetModified(); |
||
617 | } |
||
618 | |||
619 | if ($this->usersScheduledForDeletion !== null) { |
||
620 | if (!$this->usersScheduledForDeletion->isEmpty()) { |
||
621 | \Jalle19\StatusManager\Database\UserQuery::create() |
||
622 | ->filterByPrimaryKeys($this->usersScheduledForDeletion->getPrimaryKeys(false)) |
||
623 | ->delete($con); |
||
624 | $this->usersScheduledForDeletion = null; |
||
625 | } |
||
626 | } |
||
627 | |||
628 | if ($this->collUsers !== null) { |
||
629 | foreach ($this->collUsers as $referrerFK) { |
||
630 | if (!$referrerFK->isDeleted() && ($referrerFK->isNew() || $referrerFK->isModified())) { |
||
631 | $affectedRows += $referrerFK->save($con); |
||
632 | } |
||
633 | } |
||
634 | } |
||
635 | |||
636 | if ($this->connectionsScheduledForDeletion !== null) { |
||
637 | if (!$this->connectionsScheduledForDeletion->isEmpty()) { |
||
638 | \Jalle19\StatusManager\Database\ConnectionQuery::create() |
||
639 | ->filterByPrimaryKeys($this->connectionsScheduledForDeletion->getPrimaryKeys(false)) |
||
640 | ->delete($con); |
||
641 | $this->connectionsScheduledForDeletion = null; |
||
642 | } |
||
643 | } |
||
644 | |||
645 | if ($this->collConnections !== null) { |
||
646 | foreach ($this->collConnections as $referrerFK) { |
||
647 | if (!$referrerFK->isDeleted() && ($referrerFK->isNew() || $referrerFK->isModified())) { |
||
648 | $affectedRows += $referrerFK->save($con); |
||
649 | } |
||
650 | } |
||
651 | } |
||
652 | |||
653 | if ($this->channelsScheduledForDeletion !== null) { |
||
654 | if (!$this->channelsScheduledForDeletion->isEmpty()) { |
||
655 | \Jalle19\StatusManager\Database\ChannelQuery::create() |
||
656 | ->filterByPrimaryKeys($this->channelsScheduledForDeletion->getPrimaryKeys(false)) |
||
657 | ->delete($con); |
||
658 | $this->channelsScheduledForDeletion = null; |
||
659 | } |
||
660 | } |
||
661 | |||
662 | if ($this->collChannels !== null) { |
||
663 | foreach ($this->collChannels as $referrerFK) { |
||
664 | if (!$referrerFK->isDeleted() && ($referrerFK->isNew() || $referrerFK->isModified())) { |
||
665 | $affectedRows += $referrerFK->save($con); |
||
666 | } |
||
667 | } |
||
668 | } |
||
669 | |||
670 | if ($this->subscriptionsScheduledForDeletion !== null) { |
||
671 | if (!$this->subscriptionsScheduledForDeletion->isEmpty()) { |
||
672 | \Jalle19\StatusManager\Database\SubscriptionQuery::create() |
||
673 | ->filterByPrimaryKeys($this->subscriptionsScheduledForDeletion->getPrimaryKeys(false)) |
||
674 | ->delete($con); |
||
675 | $this->subscriptionsScheduledForDeletion = null; |
||
676 | } |
||
677 | } |
||
678 | |||
679 | if ($this->collSubscriptions !== null) { |
||
680 | foreach ($this->collSubscriptions as $referrerFK) { |
||
681 | if (!$referrerFK->isDeleted() && ($referrerFK->isNew() || $referrerFK->isModified())) { |
||
682 | $affectedRows += $referrerFK->save($con); |
||
683 | } |
||
684 | } |
||
685 | } |
||
686 | |||
687 | $this->alreadyInSave = false; |
||
688 | |||
689 | } |
||
690 | |||
691 | return $affectedRows; |
||
692 | } // doSave() |
||
693 | |||
694 | /** |
||
695 | * Insert the row in the database. |
||
696 | * |
||
697 | * @param ConnectionInterface $con |
||
698 | * |
||
699 | * @throws PropelException |
||
700 | * @see doSave() |
||
701 | */ |
||
702 | protected function doInsert(ConnectionInterface $con) |
||
703 | { |
||
704 | $modifiedColumns = array(); |
||
705 | $index = 0; |
||
706 | |||
707 | |||
708 | // check the columns in natural order for more readable SQL queries |
||
709 | if ($this->isColumnModified(InstanceTableMap::COL_NAME)) { |
||
710 | $modifiedColumns[':p' . $index++] = 'name'; |
||
711 | } |
||
712 | |||
713 | $sql = sprintf( |
||
714 | 'INSERT INTO instance (%s) VALUES (%s)', |
||
715 | implode(', ', $modifiedColumns), |
||
716 | implode(', ', array_keys($modifiedColumns)) |
||
717 | ); |
||
718 | |||
719 | try { |
||
720 | $stmt = $con->prepare($sql); |
||
721 | foreach ($modifiedColumns as $identifier => $columnName) { |
||
722 | switch ($columnName) { |
||
723 | case 'name': |
||
724 | $stmt->bindValue($identifier, $this->name, PDO::PARAM_STR); |
||
725 | break; |
||
726 | } |
||
727 | } |
||
728 | $stmt->execute(); |
||
729 | } catch (Exception $e) { |
||
730 | Propel::log($e->getMessage(), Propel::LOG_ERR); |
||
731 | throw new PropelException(sprintf('Unable to execute INSERT statement [%s]', $sql), 0, $e); |
||
732 | } |
||
733 | |||
734 | $this->setNew(false); |
||
735 | } |
||
736 | |||
737 | /** |
||
738 | * Update the row in the database. |
||
739 | * |
||
740 | * @param ConnectionInterface $con |
||
741 | * |
||
742 | * @return Integer Number of updated rows |
||
743 | * @see doSave() |
||
744 | */ |
||
745 | protected function doUpdate(ConnectionInterface $con) |
||
746 | { |
||
747 | $selectCriteria = $this->buildPkeyCriteria(); |
||
748 | $valuesCriteria = $this->buildCriteria(); |
||
749 | |||
750 | return $selectCriteria->doUpdate($valuesCriteria, $con); |
||
751 | } |
||
752 | |||
753 | /** |
||
754 | * Retrieves a field from the object by name passed in as a string. |
||
755 | * |
||
756 | * @param string $name name |
||
757 | * @param string $type The type of fieldname the $name is of: |
||
758 | * one of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_CAMELNAME |
||
759 | * TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM. |
||
760 | * Defaults to TableMap::TYPE_PHPNAME. |
||
761 | * @return mixed Value of field. |
||
762 | */ |
||
763 | public function getByName($name, $type = TableMap::TYPE_PHPNAME) |
||
764 | { |
||
765 | $pos = InstanceTableMap::translateFieldName($name, $type, TableMap::TYPE_NUM); |
||
766 | $field = $this->getByPosition($pos); |
||
767 | |||
768 | return $field; |
||
769 | } |
||
770 | |||
771 | /** |
||
772 | * Retrieves a field from the object by Position as specified in the xml schema. |
||
773 | * Zero-based. |
||
774 | * |
||
775 | * @param int $pos position in xml schema |
||
776 | * @return mixed Value of field at $pos |
||
777 | */ |
||
778 | public function getByPosition($pos) |
||
779 | { |
||
780 | switch ($pos) { |
||
781 | case 0: |
||
782 | return $this->getName(); |
||
783 | break; |
||
784 | default: |
||
785 | return null; |
||
786 | break; |
||
787 | } // switch() |
||
788 | } |
||
789 | |||
790 | /** |
||
791 | * Exports the object as an array. |
||
792 | * |
||
793 | * You can specify the key type of the array by passing one of the class |
||
794 | * type constants. |
||
795 | * |
||
796 | * @param string $keyType (optional) One of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_CAMELNAME, |
||
797 | * TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM. |
||
798 | * Defaults to TableMap::TYPE_PHPNAME. |
||
799 | * @param boolean $includeLazyLoadColumns (optional) Whether to include lazy loaded columns. Defaults to TRUE. |
||
800 | * @param array $alreadyDumpedObjects List of objects to skip to avoid recursion |
||
801 | * @param boolean $includeForeignObjects (optional) Whether to include hydrated related objects. Default to FALSE. |
||
802 | * |
||
803 | * @return array an associative array containing the field names (as keys) and field values |
||
804 | */ |
||
805 | public function toArray($keyType = TableMap::TYPE_PHPNAME, $includeLazyLoadColumns = true, $alreadyDumpedObjects = array(), $includeForeignObjects = false) |
||
806 | { |
||
807 | |||
808 | if (isset($alreadyDumpedObjects['Instance'][$this->hashCode()])) { |
||
809 | return '*RECURSION*'; |
||
810 | } |
||
811 | $alreadyDumpedObjects['Instance'][$this->hashCode()] = true; |
||
812 | $keys = InstanceTableMap::getFieldNames($keyType); |
||
813 | $result = array( |
||
814 | $keys[0] => $this->getName(), |
||
815 | ); |
||
816 | $virtualColumns = $this->virtualColumns; |
||
817 | foreach ($virtualColumns as $key => $virtualColumn) { |
||
818 | $result[$key] = $virtualColumn; |
||
819 | } |
||
820 | |||
821 | if ($includeForeignObjects) { |
||
822 | if (null !== $this->collUsers) { |
||
823 | |||
824 | switch ($keyType) { |
||
825 | case TableMap::TYPE_CAMELNAME: |
||
826 | $key = 'users'; |
||
827 | break; |
||
828 | case TableMap::TYPE_FIELDNAME: |
||
829 | $key = 'users'; |
||
830 | break; |
||
831 | default: |
||
832 | $key = 'Users'; |
||
833 | } |
||
834 | |||
835 | $result[$key] = $this->collUsers->toArray(null, false, $keyType, $includeLazyLoadColumns, $alreadyDumpedObjects); |
||
836 | } |
||
837 | if (null !== $this->collConnections) { |
||
838 | |||
839 | switch ($keyType) { |
||
840 | case TableMap::TYPE_CAMELNAME: |
||
841 | $key = 'connections'; |
||
842 | break; |
||
843 | case TableMap::TYPE_FIELDNAME: |
||
844 | $key = 'connections'; |
||
845 | break; |
||
846 | default: |
||
847 | $key = 'Connections'; |
||
848 | } |
||
849 | |||
850 | $result[$key] = $this->collConnections->toArray(null, false, $keyType, $includeLazyLoadColumns, $alreadyDumpedObjects); |
||
851 | } |
||
852 | if (null !== $this->collChannels) { |
||
853 | |||
854 | switch ($keyType) { |
||
855 | case TableMap::TYPE_CAMELNAME: |
||
856 | $key = 'channels'; |
||
857 | break; |
||
858 | case TableMap::TYPE_FIELDNAME: |
||
859 | $key = 'channels'; |
||
860 | break; |
||
861 | default: |
||
862 | $key = 'Channels'; |
||
863 | } |
||
864 | |||
865 | $result[$key] = $this->collChannels->toArray(null, false, $keyType, $includeLazyLoadColumns, $alreadyDumpedObjects); |
||
866 | } |
||
867 | if (null !== $this->collSubscriptions) { |
||
868 | |||
869 | switch ($keyType) { |
||
870 | case TableMap::TYPE_CAMELNAME: |
||
871 | $key = 'subscriptions'; |
||
872 | break; |
||
873 | case TableMap::TYPE_FIELDNAME: |
||
874 | $key = 'subscriptions'; |
||
875 | break; |
||
876 | default: |
||
877 | $key = 'Subscriptions'; |
||
878 | } |
||
879 | |||
880 | $result[$key] = $this->collSubscriptions->toArray(null, false, $keyType, $includeLazyLoadColumns, $alreadyDumpedObjects); |
||
881 | } |
||
882 | } |
||
883 | |||
884 | return $result; |
||
885 | } |
||
886 | |||
887 | /** |
||
888 | * Sets a field from the object by name passed in as a string. |
||
889 | * |
||
890 | * @param string $name |
||
891 | * @param mixed $value field value |
||
892 | * @param string $type The type of fieldname the $name is of: |
||
893 | * one of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_CAMELNAME |
||
894 | * TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM. |
||
895 | * Defaults to TableMap::TYPE_PHPNAME. |
||
896 | * @return $this|\Jalle19\StatusManager\Database\Instance |
||
897 | */ |
||
898 | public function setByName($name, $value, $type = TableMap::TYPE_PHPNAME) |
||
899 | { |
||
900 | $pos = InstanceTableMap::translateFieldName($name, $type, TableMap::TYPE_NUM); |
||
901 | |||
902 | return $this->setByPosition($pos, $value); |
||
903 | } |
||
904 | |||
905 | /** |
||
906 | * Sets a field from the object by Position as specified in the xml schema. |
||
907 | * Zero-based. |
||
908 | * |
||
909 | * @param int $pos position in xml schema |
||
910 | * @param mixed $value field value |
||
911 | * @return $this|\Jalle19\StatusManager\Database\Instance |
||
912 | */ |
||
913 | public function setByPosition($pos, $value) |
||
914 | { |
||
915 | switch ($pos) { |
||
916 | case 0: |
||
917 | $this->setName($value); |
||
918 | break; |
||
919 | } // switch() |
||
920 | |||
921 | return $this; |
||
922 | } |
||
923 | |||
924 | /** |
||
925 | * Populates the object using an array. |
||
926 | * |
||
927 | * This is particularly useful when populating an object from one of the |
||
928 | * request arrays (e.g. $_POST). This method goes through the column |
||
929 | * names, checking to see whether a matching key exists in populated |
||
930 | * array. If so the setByName() method is called for that column. |
||
931 | * |
||
932 | * You can specify the key type of the array by additionally passing one |
||
933 | * of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_CAMELNAME, |
||
934 | * TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM. |
||
935 | * The default key type is the column's TableMap::TYPE_PHPNAME. |
||
936 | * |
||
937 | * @param array $arr An array to populate the object from. |
||
938 | * @param string $keyType The type of keys the array uses. |
||
939 | * @return void |
||
940 | */ |
||
941 | public function fromArray($arr, $keyType = TableMap::TYPE_PHPNAME) |
||
942 | { |
||
943 | $keys = InstanceTableMap::getFieldNames($keyType); |
||
944 | |||
945 | if (array_key_exists($keys[0], $arr)) { |
||
946 | $this->setName($arr[$keys[0]]); |
||
947 | } |
||
948 | } |
||
949 | |||
950 | /** |
||
951 | * Populate the current object from a string, using a given parser format |
||
952 | * <code> |
||
953 | * $book = new Book(); |
||
954 | * $book->importFrom('JSON', '{"Id":9012,"Title":"Don Juan","ISBN":"0140422161","Price":12.99,"PublisherId":1234,"AuthorId":5678}'); |
||
955 | * </code> |
||
956 | * |
||
957 | * You can specify the key type of the array by additionally passing one |
||
958 | * of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_CAMELNAME, |
||
959 | * TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM. |
||
960 | * The default key type is the column's TableMap::TYPE_PHPNAME. |
||
961 | * |
||
962 | * @param mixed $parser A AbstractParser instance, |
||
963 | * or a format name ('XML', 'YAML', 'JSON', 'CSV') |
||
964 | * @param string $data The source data to import from |
||
965 | * @param string $keyType The type of keys the array uses. |
||
966 | * |
||
967 | * @return $this|\Jalle19\StatusManager\Database\Instance The current object, for fluid interface |
||
968 | */ |
||
969 | public function importFrom($parser, $data, $keyType = TableMap::TYPE_PHPNAME) |
||
970 | { |
||
971 | if (!$parser instanceof AbstractParser) { |
||
972 | $parser = AbstractParser::getParser($parser); |
||
973 | } |
||
974 | |||
975 | $this->fromArray($parser->toArray($data), $keyType); |
||
976 | |||
977 | return $this; |
||
978 | } |
||
979 | |||
980 | /** |
||
981 | * Build a Criteria object containing the values of all modified columns in this object. |
||
982 | * |
||
983 | * @return Criteria The Criteria object containing all modified values. |
||
984 | */ |
||
985 | public function buildCriteria() |
||
986 | { |
||
987 | $criteria = new Criteria(InstanceTableMap::DATABASE_NAME); |
||
988 | |||
989 | if ($this->isColumnModified(InstanceTableMap::COL_NAME)) { |
||
990 | $criteria->add(InstanceTableMap::COL_NAME, $this->name); |
||
991 | } |
||
992 | |||
993 | return $criteria; |
||
994 | } |
||
995 | |||
996 | /** |
||
997 | * Builds a Criteria object containing the primary key for this object. |
||
998 | * |
||
999 | * Unlike buildCriteria() this method includes the primary key values regardless |
||
1000 | * of whether or not they have been modified. |
||
1001 | * |
||
1002 | * @throws LogicException if no primary key is defined |
||
1003 | * |
||
1004 | * @return Criteria The Criteria object containing value(s) for primary key(s). |
||
1005 | */ |
||
1006 | public function buildPkeyCriteria() |
||
1007 | { |
||
1008 | $criteria = ChildInstanceQuery::create(); |
||
1009 | $criteria->add(InstanceTableMap::COL_NAME, $this->name); |
||
1010 | |||
1011 | return $criteria; |
||
1012 | } |
||
1013 | |||
1014 | /** |
||
1015 | * If the primary key is not null, return the hashcode of the |
||
1016 | * primary key. Otherwise, return the hash code of the object. |
||
1017 | * |
||
1018 | * @return int Hashcode |
||
1019 | */ |
||
1020 | public function hashCode() |
||
1021 | { |
||
1022 | $validPk = null !== $this->getName(); |
||
1023 | |||
1024 | $validPrimaryKeyFKs = 0; |
||
1025 | $primaryKeyFKs = []; |
||
1026 | |||
1027 | if ($validPk) { |
||
1028 | return crc32(json_encode($this->getPrimaryKey(), JSON_UNESCAPED_UNICODE)); |
||
1029 | } elseif ($validPrimaryKeyFKs) { |
||
1030 | return crc32(json_encode($primaryKeyFKs, JSON_UNESCAPED_UNICODE)); |
||
1031 | } |
||
1032 | |||
1033 | return spl_object_hash($this); |
||
1034 | } |
||
1035 | |||
1036 | /** |
||
1037 | * Returns the primary key for this object (row). |
||
1038 | * @return string |
||
1039 | */ |
||
1040 | public function getPrimaryKey() |
||
1041 | { |
||
1042 | return $this->getName(); |
||
1043 | } |
||
1044 | |||
1045 | /** |
||
1046 | * Generic method to set the primary key (name column). |
||
1047 | * |
||
1048 | * @param string $key Primary key. |
||
1049 | * @return void |
||
1050 | */ |
||
1051 | public function setPrimaryKey($key) |
||
1052 | { |
||
1053 | $this->setName($key); |
||
1054 | } |
||
1055 | |||
1056 | /** |
||
1057 | * Returns true if the primary key for this object is null. |
||
1058 | * @return boolean |
||
1059 | */ |
||
1060 | public function isPrimaryKeyNull() |
||
1061 | { |
||
1062 | return null === $this->getName(); |
||
1063 | } |
||
1064 | |||
1065 | /** |
||
1066 | * Sets contents of passed object to values from current object. |
||
1067 | * |
||
1068 | * If desired, this method can also make copies of all associated (fkey referrers) |
||
1069 | * objects. |
||
1070 | * |
||
1071 | * @param object $copyObj An object of \Jalle19\StatusManager\Database\Instance (or compatible) type. |
||
1072 | * @param boolean $deepCopy Whether to also copy all rows that refer (by fkey) to the current row. |
||
1073 | * @param boolean $makeNew Whether to reset autoincrement PKs and make the object new. |
||
1074 | * @throws PropelException |
||
1075 | */ |
||
1076 | public function copyInto($copyObj, $deepCopy = false, $makeNew = true) |
||
1077 | { |
||
1078 | $copyObj->setName($this->getName()); |
||
1079 | |||
1080 | if ($deepCopy) { |
||
1081 | // important: temporarily setNew(false) because this affects the behavior of |
||
1082 | // the getter/setter methods for fkey referrer objects. |
||
1083 | $copyObj->setNew(false); |
||
1084 | |||
1085 | foreach ($this->getUsers() as $relObj) { |
||
1086 | if ($relObj !== $this) { // ensure that we don't try to copy a reference to ourselves |
||
1087 | $copyObj->addUser($relObj->copy($deepCopy)); |
||
1088 | } |
||
1089 | } |
||
1090 | |||
1091 | foreach ($this->getConnections() as $relObj) { |
||
1092 | if ($relObj !== $this) { // ensure that we don't try to copy a reference to ourselves |
||
1093 | $copyObj->addConnection($relObj->copy($deepCopy)); |
||
1094 | } |
||
1095 | } |
||
1096 | |||
1097 | foreach ($this->getChannels() as $relObj) { |
||
1098 | if ($relObj !== $this) { // ensure that we don't try to copy a reference to ourselves |
||
1099 | $copyObj->addChannel($relObj->copy($deepCopy)); |
||
1100 | } |
||
1101 | } |
||
1102 | |||
1103 | foreach ($this->getSubscriptions() as $relObj) { |
||
1104 | if ($relObj !== $this) { // ensure that we don't try to copy a reference to ourselves |
||
1105 | $copyObj->addSubscription($relObj->copy($deepCopy)); |
||
1106 | } |
||
1107 | } |
||
1108 | |||
1109 | } // if ($deepCopy) |
||
1110 | |||
1111 | if ($makeNew) { |
||
1112 | $copyObj->setNew(true); |
||
1113 | } |
||
1114 | } |
||
1115 | |||
1116 | /** |
||
1117 | * Makes a copy of this object that will be inserted as a new row in table when saved. |
||
1118 | * It creates a new object filling in the simple attributes, but skipping any primary |
||
1119 | * keys that are defined for the table. |
||
1120 | * |
||
1121 | * If desired, this method can also make copies of all associated (fkey referrers) |
||
1122 | * objects. |
||
1123 | * |
||
1124 | * @param boolean $deepCopy Whether to also copy all rows that refer (by fkey) to the current row. |
||
1125 | * @return \Jalle19\StatusManager\Database\Instance Clone of current object. |
||
1126 | * @throws PropelException |
||
1127 | */ |
||
1128 | public function copy($deepCopy = false) |
||
1129 | { |
||
1130 | // we use get_class(), because this might be a subclass |
||
1131 | $clazz = get_class($this); |
||
1132 | $copyObj = new $clazz(); |
||
1133 | $this->copyInto($copyObj, $deepCopy); |
||
1134 | |||
1135 | return $copyObj; |
||
1136 | } |
||
1137 | |||
1138 | |||
1139 | /** |
||
1140 | * Initializes a collection based on the name of a relation. |
||
1141 | * Avoids crafting an 'init[$relationName]s' method name |
||
1142 | * that wouldn't work when StandardEnglishPluralizer is used. |
||
1143 | * |
||
1144 | * @param string $relationName The name of the relation to initialize |
||
1145 | * @return void |
||
1146 | */ |
||
1147 | public function initRelation($relationName) |
||
1148 | { |
||
1149 | if ('User' == $relationName) { |
||
1150 | return $this->initUsers(); |
||
1151 | } |
||
1152 | if ('Connection' == $relationName) { |
||
1153 | return $this->initConnections(); |
||
1154 | } |
||
1155 | if ('Channel' == $relationName) { |
||
1156 | return $this->initChannels(); |
||
1157 | } |
||
1158 | if ('Subscription' == $relationName) { |
||
1159 | return $this->initSubscriptions(); |
||
1160 | } |
||
1161 | } |
||
1162 | |||
1163 | /** |
||
1164 | * Clears out the collUsers collection |
||
1165 | * |
||
1166 | * This does not modify the database; however, it will remove any associated objects, causing |
||
1167 | * them to be refetched by subsequent calls to accessor method. |
||
1168 | * |
||
1169 | * @return void |
||
1170 | * @see addUsers() |
||
1171 | */ |
||
1172 | public function clearUsers() |
||
1173 | { |
||
1174 | $this->collUsers = null; // important to set this to NULL since that means it is uninitialized |
||
1175 | } |
||
1176 | |||
1177 | /** |
||
1178 | * Reset is the collUsers collection loaded partially. |
||
1179 | */ |
||
1180 | public function resetPartialUsers($v = true) |
||
1181 | { |
||
1182 | $this->collUsersPartial = $v; |
||
1183 | } |
||
1184 | |||
1185 | /** |
||
1186 | * Initializes the collUsers collection. |
||
1187 | * |
||
1188 | * By default this just sets the collUsers collection to an empty array (like clearcollUsers()); |
||
1189 | * however, you may wish to override this method in your stub class to provide setting appropriate |
||
1190 | * to your application -- for example, setting the initial array to the values stored in database. |
||
1191 | * |
||
1192 | * @param boolean $overrideExisting If set to true, the method call initializes |
||
1193 | * the collection even if it is not empty |
||
1194 | * |
||
1195 | * @return void |
||
1196 | */ |
||
1197 | public function initUsers($overrideExisting = true) |
||
1198 | { |
||
1199 | if (null !== $this->collUsers && !$overrideExisting) { |
||
1200 | return; |
||
1201 | } |
||
1202 | |||
1203 | $collectionClassName = UserTableMap::getTableMap()->getCollectionClassName(); |
||
1204 | |||
1205 | $this->collUsers = new $collectionClassName; |
||
1206 | $this->collUsers->setModel('\Jalle19\StatusManager\Database\User'); |
||
1207 | } |
||
1208 | |||
1209 | /** |
||
1210 | * Gets an array of ChildUser objects which contain a foreign key that references this object. |
||
1211 | * |
||
1212 | * If the $criteria is not null, it is used to always fetch the results from the database. |
||
1213 | * Otherwise the results are fetched from the database the first time, then cached. |
||
1214 | * Next time the same method is called without $criteria, the cached collection is returned. |
||
1215 | * If this ChildInstance is new, it will return |
||
1216 | * an empty collection or the current collection; the criteria is ignored on a new object. |
||
1217 | * |
||
1218 | * @param Criteria $criteria optional Criteria object to narrow the query |
||
1219 | * @param ConnectionInterface $con optional connection object |
||
1220 | * @return ObjectCollection|ChildUser[] List of ChildUser objects |
||
1221 | * @throws PropelException |
||
1222 | */ |
||
1223 | public function getUsers(Criteria $criteria = null, ConnectionInterface $con = null) |
||
1224 | { |
||
1225 | $partial = $this->collUsersPartial && !$this->isNew(); |
||
1226 | if (null === $this->collUsers || null !== $criteria || $partial) { |
||
1227 | if ($this->isNew() && null === $this->collUsers) { |
||
1228 | // return empty collection |
||
1229 | $this->initUsers(); |
||
1230 | } else { |
||
1231 | $collUsers = ChildUserQuery::create(null, $criteria) |
||
1232 | ->filterByInstance($this) |
||
1233 | ->find($con); |
||
1234 | |||
1235 | if (null !== $criteria) { |
||
1236 | if (false !== $this->collUsersPartial && count($collUsers)) { |
||
1237 | $this->initUsers(false); |
||
1238 | |||
1239 | foreach ($collUsers as $obj) { |
||
1240 | if (false == $this->collUsers->contains($obj)) { |
||
1241 | $this->collUsers->append($obj); |
||
1242 | } |
||
1243 | } |
||
1244 | |||
1245 | $this->collUsersPartial = true; |
||
1246 | } |
||
1247 | |||
1248 | return $collUsers; |
||
1249 | } |
||
1250 | |||
1251 | if ($partial && $this->collUsers) { |
||
1252 | foreach ($this->collUsers as $obj) { |
||
1253 | if ($obj->isNew()) { |
||
1254 | $collUsers[] = $obj; |
||
1255 | } |
||
1256 | } |
||
1257 | } |
||
1258 | |||
1259 | $this->collUsers = $collUsers; |
||
1260 | $this->collUsersPartial = false; |
||
1261 | } |
||
1262 | } |
||
1263 | |||
1264 | return $this->collUsers; |
||
1265 | } |
||
1266 | |||
1267 | /** |
||
1268 | * Sets a collection of ChildUser objects related by a one-to-many relationship |
||
1269 | * to the current object. |
||
1270 | * It will also schedule objects for deletion based on a diff between old objects (aka persisted) |
||
1271 | * and new objects from the given Propel collection. |
||
1272 | * |
||
1273 | * @param Collection $users A Propel collection. |
||
1274 | * @param ConnectionInterface $con Optional connection object |
||
1275 | * @return $this|ChildInstance The current object (for fluent API support) |
||
1276 | */ |
||
1277 | public function setUsers(Collection $users, ConnectionInterface $con = null) |
||
1278 | { |
||
1279 | /** @var ChildUser[] $usersToDelete */ |
||
1280 | $usersToDelete = $this->getUsers(new Criteria(), $con)->diff($users); |
||
1281 | |||
1282 | |||
1283 | $this->usersScheduledForDeletion = $usersToDelete; |
||
1284 | |||
1285 | foreach ($usersToDelete as $userRemoved) { |
||
1286 | $userRemoved->setInstance(null); |
||
1287 | } |
||
1288 | |||
1289 | $this->collUsers = null; |
||
1290 | foreach ($users as $user) { |
||
1291 | $this->addUser($user); |
||
1292 | } |
||
1293 | |||
1294 | $this->collUsers = $users; |
||
1295 | $this->collUsersPartial = false; |
||
1296 | |||
1297 | return $this; |
||
1298 | } |
||
1299 | |||
1300 | /** |
||
1301 | * Returns the number of related User objects. |
||
1302 | * |
||
1303 | * @param Criteria $criteria |
||
1304 | * @param boolean $distinct |
||
1305 | * @param ConnectionInterface $con |
||
1306 | * @return int Count of related User objects. |
||
1307 | * @throws PropelException |
||
1308 | */ |
||
1309 | public function countUsers(Criteria $criteria = null, $distinct = false, ConnectionInterface $con = null) |
||
1310 | { |
||
1311 | $partial = $this->collUsersPartial && !$this->isNew(); |
||
1312 | if (null === $this->collUsers || null !== $criteria || $partial) { |
||
1313 | if ($this->isNew() && null === $this->collUsers) { |
||
1314 | return 0; |
||
1315 | } |
||
1316 | |||
1317 | if ($partial && !$criteria) { |
||
1318 | return count($this->getUsers()); |
||
1319 | } |
||
1320 | |||
1321 | $query = ChildUserQuery::create(null, $criteria); |
||
1322 | if ($distinct) { |
||
1323 | $query->distinct(); |
||
1324 | } |
||
1325 | |||
1326 | return $query |
||
1327 | ->filterByInstance($this) |
||
1328 | ->count($con); |
||
1329 | } |
||
1330 | |||
1331 | return count($this->collUsers); |
||
1332 | } |
||
1333 | |||
1334 | /** |
||
1335 | * Method called to associate a ChildUser object to this object |
||
1336 | * through the ChildUser foreign key attribute. |
||
1337 | * |
||
1338 | * @param ChildUser $l ChildUser |
||
1339 | * @return $this|\Jalle19\StatusManager\Database\Instance The current object (for fluent API support) |
||
1340 | */ |
||
1341 | public function addUser(ChildUser $l) |
||
1342 | { |
||
1343 | if ($this->collUsers === null) { |
||
1344 | $this->initUsers(); |
||
1345 | $this->collUsersPartial = true; |
||
1346 | } |
||
1347 | |||
1348 | if (!$this->collUsers->contains($l)) { |
||
1349 | $this->doAddUser($l); |
||
1350 | |||
1351 | if ($this->usersScheduledForDeletion and $this->usersScheduledForDeletion->contains($l)) { |
||
1352 | $this->usersScheduledForDeletion->remove($this->usersScheduledForDeletion->search($l)); |
||
1353 | } |
||
1354 | } |
||
1355 | |||
1356 | return $this; |
||
1357 | } |
||
1358 | |||
1359 | /** |
||
1360 | * @param ChildUser $user The ChildUser object to add. |
||
1361 | */ |
||
1362 | protected function doAddUser(ChildUser $user) |
||
1363 | { |
||
1364 | $this->collUsers[]= $user; |
||
1365 | $user->setInstance($this); |
||
1366 | } |
||
1367 | |||
1368 | /** |
||
1369 | * @param ChildUser $user The ChildUser object to remove. |
||
1370 | * @return $this|ChildInstance The current object (for fluent API support) |
||
1371 | */ |
||
1372 | public function removeUser(ChildUser $user) |
||
1373 | { |
||
1374 | if ($this->getUsers()->contains($user)) { |
||
1375 | $pos = $this->collUsers->search($user); |
||
1376 | $this->collUsers->remove($pos); |
||
1377 | if (null === $this->usersScheduledForDeletion) { |
||
1378 | $this->usersScheduledForDeletion = clone $this->collUsers; |
||
1379 | $this->usersScheduledForDeletion->clear(); |
||
1380 | } |
||
1381 | $this->usersScheduledForDeletion[]= clone $user; |
||
1382 | $user->setInstance(null); |
||
1383 | } |
||
1384 | |||
1385 | return $this; |
||
1386 | } |
||
1387 | |||
1388 | /** |
||
1389 | * Clears out the collConnections collection |
||
1390 | * |
||
1391 | * This does not modify the database; however, it will remove any associated objects, causing |
||
1392 | * them to be refetched by subsequent calls to accessor method. |
||
1393 | * |
||
1394 | * @return void |
||
1395 | * @see addConnections() |
||
1396 | */ |
||
1397 | public function clearConnections() |
||
1398 | { |
||
1399 | $this->collConnections = null; // important to set this to NULL since that means it is uninitialized |
||
1400 | } |
||
1401 | |||
1402 | /** |
||
1403 | * Reset is the collConnections collection loaded partially. |
||
1404 | */ |
||
1405 | public function resetPartialConnections($v = true) |
||
1406 | { |
||
1407 | $this->collConnectionsPartial = $v; |
||
1408 | } |
||
1409 | |||
1410 | /** |
||
1411 | * Initializes the collConnections collection. |
||
1412 | * |
||
1413 | * By default this just sets the collConnections collection to an empty array (like clearcollConnections()); |
||
1414 | * however, you may wish to override this method in your stub class to provide setting appropriate |
||
1415 | * to your application -- for example, setting the initial array to the values stored in database. |
||
1416 | * |
||
1417 | * @param boolean $overrideExisting If set to true, the method call initializes |
||
1418 | * the collection even if it is not empty |
||
1419 | * |
||
1420 | * @return void |
||
1421 | */ |
||
1422 | public function initConnections($overrideExisting = true) |
||
1423 | { |
||
1424 | if (null !== $this->collConnections && !$overrideExisting) { |
||
1425 | return; |
||
1426 | } |
||
1427 | |||
1428 | $collectionClassName = ConnectionTableMap::getTableMap()->getCollectionClassName(); |
||
1429 | |||
1430 | $this->collConnections = new $collectionClassName; |
||
1431 | $this->collConnections->setModel('\Jalle19\StatusManager\Database\Connection'); |
||
1432 | } |
||
1433 | |||
1434 | /** |
||
1435 | * Gets an array of ChildConnection objects which contain a foreign key that references this object. |
||
1436 | * |
||
1437 | * If the $criteria is not null, it is used to always fetch the results from the database. |
||
1438 | * Otherwise the results are fetched from the database the first time, then cached. |
||
1439 | * Next time the same method is called without $criteria, the cached collection is returned. |
||
1440 | * If this ChildInstance is new, it will return |
||
1441 | * an empty collection or the current collection; the criteria is ignored on a new object. |
||
1442 | * |
||
1443 | * @param Criteria $criteria optional Criteria object to narrow the query |
||
1444 | * @param ConnectionInterface $con optional connection object |
||
1445 | * @return ObjectCollection|ChildConnection[] List of ChildConnection objects |
||
1446 | * @throws PropelException |
||
1447 | */ |
||
1448 | public function getConnections(Criteria $criteria = null, ConnectionInterface $con = null) |
||
1449 | { |
||
1450 | $partial = $this->collConnectionsPartial && !$this->isNew(); |
||
1451 | if (null === $this->collConnections || null !== $criteria || $partial) { |
||
1452 | if ($this->isNew() && null === $this->collConnections) { |
||
1453 | // return empty collection |
||
1454 | $this->initConnections(); |
||
1455 | } else { |
||
1456 | $collConnections = ChildConnectionQuery::create(null, $criteria) |
||
1457 | ->filterByInstance($this) |
||
1458 | ->find($con); |
||
1459 | |||
1460 | if (null !== $criteria) { |
||
1461 | if (false !== $this->collConnectionsPartial && count($collConnections)) { |
||
1462 | $this->initConnections(false); |
||
1463 | |||
1464 | foreach ($collConnections as $obj) { |
||
1465 | if (false == $this->collConnections->contains($obj)) { |
||
1466 | $this->collConnections->append($obj); |
||
1467 | } |
||
1468 | } |
||
1469 | |||
1470 | $this->collConnectionsPartial = true; |
||
1471 | } |
||
1472 | |||
1473 | return $collConnections; |
||
1474 | } |
||
1475 | |||
1476 | if ($partial && $this->collConnections) { |
||
1477 | foreach ($this->collConnections as $obj) { |
||
1478 | if ($obj->isNew()) { |
||
1479 | $collConnections[] = $obj; |
||
1480 | } |
||
1481 | } |
||
1482 | } |
||
1483 | |||
1484 | $this->collConnections = $collConnections; |
||
1485 | $this->collConnectionsPartial = false; |
||
1486 | } |
||
1487 | } |
||
1488 | |||
1489 | return $this->collConnections; |
||
1490 | } |
||
1491 | |||
1492 | /** |
||
1493 | * Sets a collection of ChildConnection objects related by a one-to-many relationship |
||
1494 | * to the current object. |
||
1495 | * It will also schedule objects for deletion based on a diff between old objects (aka persisted) |
||
1496 | * and new objects from the given Propel collection. |
||
1497 | * |
||
1498 | * @param Collection $connections A Propel collection. |
||
1499 | * @param ConnectionInterface $con Optional connection object |
||
1500 | * @return $this|ChildInstance The current object (for fluent API support) |
||
1501 | */ |
||
1502 | public function setConnections(Collection $connections, ConnectionInterface $con = null) |
||
1503 | { |
||
1504 | /** @var ChildConnection[] $connectionsToDelete */ |
||
1505 | $connectionsToDelete = $this->getConnections(new Criteria(), $con)->diff($connections); |
||
1506 | |||
1507 | |||
1508 | $this->connectionsScheduledForDeletion = $connectionsToDelete; |
||
1509 | |||
1510 | foreach ($connectionsToDelete as $connectionRemoved) { |
||
1511 | $connectionRemoved->setInstance(null); |
||
1512 | } |
||
1513 | |||
1514 | $this->collConnections = null; |
||
1515 | foreach ($connections as $connection) { |
||
1516 | $this->addConnection($connection); |
||
1517 | } |
||
1518 | |||
1519 | $this->collConnections = $connections; |
||
1520 | $this->collConnectionsPartial = false; |
||
1521 | |||
1522 | return $this; |
||
1523 | } |
||
1524 | |||
1525 | /** |
||
1526 | * Returns the number of related Connection objects. |
||
1527 | * |
||
1528 | * @param Criteria $criteria |
||
1529 | * @param boolean $distinct |
||
1530 | * @param ConnectionInterface $con |
||
1531 | * @return int Count of related Connection objects. |
||
1532 | * @throws PropelException |
||
1533 | */ |
||
1534 | public function countConnections(Criteria $criteria = null, $distinct = false, ConnectionInterface $con = null) |
||
1535 | { |
||
1536 | $partial = $this->collConnectionsPartial && !$this->isNew(); |
||
1537 | if (null === $this->collConnections || null !== $criteria || $partial) { |
||
1538 | if ($this->isNew() && null === $this->collConnections) { |
||
1539 | return 0; |
||
1540 | } |
||
1541 | |||
1542 | if ($partial && !$criteria) { |
||
1543 | return count($this->getConnections()); |
||
1544 | } |
||
1545 | |||
1546 | $query = ChildConnectionQuery::create(null, $criteria); |
||
1547 | if ($distinct) { |
||
1548 | $query->distinct(); |
||
1549 | } |
||
1550 | |||
1551 | return $query |
||
1552 | ->filterByInstance($this) |
||
1553 | ->count($con); |
||
1554 | } |
||
1555 | |||
1556 | return count($this->collConnections); |
||
1557 | } |
||
1558 | |||
1559 | /** |
||
1560 | * Method called to associate a ChildConnection object to this object |
||
1561 | * through the ChildConnection foreign key attribute. |
||
1562 | * |
||
1563 | * @param ChildConnection $l ChildConnection |
||
1564 | * @return $this|\Jalle19\StatusManager\Database\Instance The current object (for fluent API support) |
||
1565 | */ |
||
1566 | public function addConnection(ChildConnection $l) |
||
1567 | { |
||
1568 | if ($this->collConnections === null) { |
||
1569 | $this->initConnections(); |
||
1570 | $this->collConnectionsPartial = true; |
||
1571 | } |
||
1572 | |||
1573 | if (!$this->collConnections->contains($l)) { |
||
1574 | $this->doAddConnection($l); |
||
1575 | |||
1576 | if ($this->connectionsScheduledForDeletion and $this->connectionsScheduledForDeletion->contains($l)) { |
||
1577 | $this->connectionsScheduledForDeletion->remove($this->connectionsScheduledForDeletion->search($l)); |
||
1578 | } |
||
1579 | } |
||
1580 | |||
1581 | return $this; |
||
1582 | } |
||
1583 | |||
1584 | /** |
||
1585 | * @param ChildConnection $connection The ChildConnection object to add. |
||
1586 | */ |
||
1587 | protected function doAddConnection(ChildConnection $connection) |
||
1588 | { |
||
1589 | $this->collConnections[]= $connection; |
||
1590 | $connection->setInstance($this); |
||
1591 | } |
||
1592 | |||
1593 | /** |
||
1594 | * @param ChildConnection $connection The ChildConnection object to remove. |
||
1595 | * @return $this|ChildInstance The current object (for fluent API support) |
||
1596 | */ |
||
1597 | public function removeConnection(ChildConnection $connection) |
||
1598 | { |
||
1599 | if ($this->getConnections()->contains($connection)) { |
||
1600 | $pos = $this->collConnections->search($connection); |
||
1601 | $this->collConnections->remove($pos); |
||
1602 | if (null === $this->connectionsScheduledForDeletion) { |
||
1603 | $this->connectionsScheduledForDeletion = clone $this->collConnections; |
||
1604 | $this->connectionsScheduledForDeletion->clear(); |
||
1605 | } |
||
1606 | $this->connectionsScheduledForDeletion[]= clone $connection; |
||
1607 | $connection->setInstance(null); |
||
1608 | } |
||
1609 | |||
1610 | return $this; |
||
1611 | } |
||
1612 | |||
1613 | |||
1614 | /** |
||
1615 | * If this collection has already been initialized with |
||
1616 | * an identical criteria, it returns the collection. |
||
1617 | * Otherwise if this Instance is new, it will return |
||
1618 | * an empty collection; or if this Instance has previously |
||
1619 | * been saved, it will retrieve related Connections from storage. |
||
1620 | * |
||
1621 | * This method is protected by default in order to keep the public |
||
1622 | * api reasonable. You can provide public methods for those you |
||
1623 | * actually need in Instance. |
||
1624 | * |
||
1625 | * @param Criteria $criteria optional Criteria object to narrow the query |
||
1626 | * @param ConnectionInterface $con optional connection object |
||
1627 | * @param string $joinBehavior optional join type to use (defaults to Criteria::LEFT_JOIN) |
||
1628 | * @return ObjectCollection|ChildConnection[] List of ChildConnection objects |
||
1629 | */ |
||
1630 | public function getConnectionsJoinUser(Criteria $criteria = null, ConnectionInterface $con = null, $joinBehavior = Criteria::LEFT_JOIN) |
||
1631 | { |
||
1632 | $query = ChildConnectionQuery::create(null, $criteria); |
||
1633 | $query->joinWith('User', $joinBehavior); |
||
1634 | |||
1635 | return $this->getConnections($query, $con); |
||
1636 | } |
||
1637 | |||
1638 | /** |
||
1639 | * Clears out the collChannels collection |
||
1640 | * |
||
1641 | * This does not modify the database; however, it will remove any associated objects, causing |
||
1642 | * them to be refetched by subsequent calls to accessor method. |
||
1643 | * |
||
1644 | * @return void |
||
1645 | * @see addChannels() |
||
1646 | */ |
||
1647 | public function clearChannels() |
||
1648 | { |
||
1649 | $this->collChannels = null; // important to set this to NULL since that means it is uninitialized |
||
1650 | } |
||
1651 | |||
1652 | /** |
||
1653 | * Reset is the collChannels collection loaded partially. |
||
1654 | */ |
||
1655 | public function resetPartialChannels($v = true) |
||
1656 | { |
||
1657 | $this->collChannelsPartial = $v; |
||
1658 | } |
||
1659 | |||
1660 | /** |
||
1661 | * Initializes the collChannels collection. |
||
1662 | * |
||
1663 | * By default this just sets the collChannels collection to an empty array (like clearcollChannels()); |
||
1664 | * however, you may wish to override this method in your stub class to provide setting appropriate |
||
1665 | * to your application -- for example, setting the initial array to the values stored in database. |
||
1666 | * |
||
1667 | * @param boolean $overrideExisting If set to true, the method call initializes |
||
1668 | * the collection even if it is not empty |
||
1669 | * |
||
1670 | * @return void |
||
1671 | */ |
||
1672 | public function initChannels($overrideExisting = true) |
||
1673 | { |
||
1674 | if (null !== $this->collChannels && !$overrideExisting) { |
||
1675 | return; |
||
1676 | } |
||
1677 | |||
1678 | $collectionClassName = ChannelTableMap::getTableMap()->getCollectionClassName(); |
||
1679 | |||
1680 | $this->collChannels = new $collectionClassName; |
||
1681 | $this->collChannels->setModel('\Jalle19\StatusManager\Database\Channel'); |
||
1682 | } |
||
1683 | |||
1684 | /** |
||
1685 | * Gets an array of ChildChannel objects which contain a foreign key that references this object. |
||
1686 | * |
||
1687 | * If the $criteria is not null, it is used to always fetch the results from the database. |
||
1688 | * Otherwise the results are fetched from the database the first time, then cached. |
||
1689 | * Next time the same method is called without $criteria, the cached collection is returned. |
||
1690 | * If this ChildInstance is new, it will return |
||
1691 | * an empty collection or the current collection; the criteria is ignored on a new object. |
||
1692 | * |
||
1693 | * @param Criteria $criteria optional Criteria object to narrow the query |
||
1694 | * @param ConnectionInterface $con optional connection object |
||
1695 | * @return ObjectCollection|ChildChannel[] List of ChildChannel objects |
||
1696 | * @throws PropelException |
||
1697 | */ |
||
1698 | public function getChannels(Criteria $criteria = null, ConnectionInterface $con = null) |
||
1699 | { |
||
1700 | $partial = $this->collChannelsPartial && !$this->isNew(); |
||
1701 | if (null === $this->collChannels || null !== $criteria || $partial) { |
||
1702 | if ($this->isNew() && null === $this->collChannels) { |
||
1703 | // return empty collection |
||
1704 | $this->initChannels(); |
||
1705 | } else { |
||
1706 | $collChannels = ChildChannelQuery::create(null, $criteria) |
||
1707 | ->filterByInstance($this) |
||
1708 | ->find($con); |
||
1709 | |||
1710 | if (null !== $criteria) { |
||
1711 | if (false !== $this->collChannelsPartial && count($collChannels)) { |
||
1712 | $this->initChannels(false); |
||
1713 | |||
1714 | foreach ($collChannels as $obj) { |
||
1715 | if (false == $this->collChannels->contains($obj)) { |
||
1716 | $this->collChannels->append($obj); |
||
1717 | } |
||
1718 | } |
||
1719 | |||
1720 | $this->collChannelsPartial = true; |
||
1721 | } |
||
1722 | |||
1723 | return $collChannels; |
||
1724 | } |
||
1725 | |||
1726 | if ($partial && $this->collChannels) { |
||
1727 | foreach ($this->collChannels as $obj) { |
||
1728 | if ($obj->isNew()) { |
||
1729 | $collChannels[] = $obj; |
||
1730 | } |
||
1731 | } |
||
1732 | } |
||
1733 | |||
1734 | $this->collChannels = $collChannels; |
||
1735 | $this->collChannelsPartial = false; |
||
1736 | } |
||
1737 | } |
||
1738 | |||
1739 | return $this->collChannels; |
||
1740 | } |
||
1741 | |||
1742 | /** |
||
1743 | * Sets a collection of ChildChannel objects related by a one-to-many relationship |
||
1744 | * to the current object. |
||
1745 | * It will also schedule objects for deletion based on a diff between old objects (aka persisted) |
||
1746 | * and new objects from the given Propel collection. |
||
1747 | * |
||
1748 | * @param Collection $channels A Propel collection. |
||
1749 | * @param ConnectionInterface $con Optional connection object |
||
1750 | * @return $this|ChildInstance The current object (for fluent API support) |
||
1751 | */ |
||
1752 | public function setChannels(Collection $channels, ConnectionInterface $con = null) |
||
1753 | { |
||
1754 | /** @var ChildChannel[] $channelsToDelete */ |
||
1755 | $channelsToDelete = $this->getChannels(new Criteria(), $con)->diff($channels); |
||
1756 | |||
1757 | |||
1758 | $this->channelsScheduledForDeletion = $channelsToDelete; |
||
1759 | |||
1760 | foreach ($channelsToDelete as $channelRemoved) { |
||
1761 | $channelRemoved->setInstance(null); |
||
1762 | } |
||
1763 | |||
1764 | $this->collChannels = null; |
||
1765 | foreach ($channels as $channel) { |
||
1766 | $this->addChannel($channel); |
||
1767 | } |
||
1768 | |||
1769 | $this->collChannels = $channels; |
||
1770 | $this->collChannelsPartial = false; |
||
1771 | |||
1772 | return $this; |
||
1773 | } |
||
1774 | |||
1775 | /** |
||
1776 | * Returns the number of related Channel objects. |
||
1777 | * |
||
1778 | * @param Criteria $criteria |
||
1779 | * @param boolean $distinct |
||
1780 | * @param ConnectionInterface $con |
||
1781 | * @return int Count of related Channel objects. |
||
1782 | * @throws PropelException |
||
1783 | */ |
||
1784 | public function countChannels(Criteria $criteria = null, $distinct = false, ConnectionInterface $con = null) |
||
1785 | { |
||
1786 | $partial = $this->collChannelsPartial && !$this->isNew(); |
||
1787 | if (null === $this->collChannels || null !== $criteria || $partial) { |
||
1788 | if ($this->isNew() && null === $this->collChannels) { |
||
1789 | return 0; |
||
1790 | } |
||
1791 | |||
1792 | if ($partial && !$criteria) { |
||
1793 | return count($this->getChannels()); |
||
1794 | } |
||
1795 | |||
1796 | $query = ChildChannelQuery::create(null, $criteria); |
||
1797 | if ($distinct) { |
||
1798 | $query->distinct(); |
||
1799 | } |
||
1800 | |||
1801 | return $query |
||
1802 | ->filterByInstance($this) |
||
1803 | ->count($con); |
||
1804 | } |
||
1805 | |||
1806 | return count($this->collChannels); |
||
1807 | } |
||
1808 | |||
1809 | /** |
||
1810 | * Method called to associate a ChildChannel object to this object |
||
1811 | * through the ChildChannel foreign key attribute. |
||
1812 | * |
||
1813 | * @param ChildChannel $l ChildChannel |
||
1814 | * @return $this|\Jalle19\StatusManager\Database\Instance The current object (for fluent API support) |
||
1815 | */ |
||
1816 | public function addChannel(ChildChannel $l) |
||
1817 | { |
||
1818 | if ($this->collChannels === null) { |
||
1819 | $this->initChannels(); |
||
1820 | $this->collChannelsPartial = true; |
||
1821 | } |
||
1822 | |||
1823 | if (!$this->collChannels->contains($l)) { |
||
1824 | $this->doAddChannel($l); |
||
1825 | |||
1826 | if ($this->channelsScheduledForDeletion and $this->channelsScheduledForDeletion->contains($l)) { |
||
1827 | $this->channelsScheduledForDeletion->remove($this->channelsScheduledForDeletion->search($l)); |
||
1828 | } |
||
1829 | } |
||
1830 | |||
1831 | return $this; |
||
1832 | } |
||
1833 | |||
1834 | /** |
||
1835 | * @param ChildChannel $channel The ChildChannel object to add. |
||
1836 | */ |
||
1837 | protected function doAddChannel(ChildChannel $channel) |
||
1838 | { |
||
1839 | $this->collChannels[]= $channel; |
||
1840 | $channel->setInstance($this); |
||
1841 | } |
||
1842 | |||
1843 | /** |
||
1844 | * @param ChildChannel $channel The ChildChannel object to remove. |
||
1845 | * @return $this|ChildInstance The current object (for fluent API support) |
||
1846 | */ |
||
1847 | public function removeChannel(ChildChannel $channel) |
||
1848 | { |
||
1849 | if ($this->getChannels()->contains($channel)) { |
||
1850 | $pos = $this->collChannels->search($channel); |
||
1851 | $this->collChannels->remove($pos); |
||
1852 | if (null === $this->channelsScheduledForDeletion) { |
||
1853 | $this->channelsScheduledForDeletion = clone $this->collChannels; |
||
1854 | $this->channelsScheduledForDeletion->clear(); |
||
1855 | } |
||
1856 | $this->channelsScheduledForDeletion[]= clone $channel; |
||
1857 | $channel->setInstance(null); |
||
1858 | } |
||
1859 | |||
1860 | return $this; |
||
1861 | } |
||
1862 | |||
1863 | /** |
||
1864 | * Clears out the collSubscriptions collection |
||
1865 | * |
||
1866 | * This does not modify the database; however, it will remove any associated objects, causing |
||
1867 | * them to be refetched by subsequent calls to accessor method. |
||
1868 | * |
||
1869 | * @return void |
||
1870 | * @see addSubscriptions() |
||
1871 | */ |
||
1872 | public function clearSubscriptions() |
||
1873 | { |
||
1874 | $this->collSubscriptions = null; // important to set this to NULL since that means it is uninitialized |
||
1875 | } |
||
1876 | |||
1877 | /** |
||
1878 | * Reset is the collSubscriptions collection loaded partially. |
||
1879 | */ |
||
1880 | public function resetPartialSubscriptions($v = true) |
||
1881 | { |
||
1882 | $this->collSubscriptionsPartial = $v; |
||
1883 | } |
||
1884 | |||
1885 | /** |
||
1886 | * Initializes the collSubscriptions collection. |
||
1887 | * |
||
1888 | * By default this just sets the collSubscriptions collection to an empty array (like clearcollSubscriptions()); |
||
1889 | * however, you may wish to override this method in your stub class to provide setting appropriate |
||
1890 | * to your application -- for example, setting the initial array to the values stored in database. |
||
1891 | * |
||
1892 | * @param boolean $overrideExisting If set to true, the method call initializes |
||
1893 | * the collection even if it is not empty |
||
1894 | * |
||
1895 | * @return void |
||
1896 | */ |
||
1897 | public function initSubscriptions($overrideExisting = true) |
||
1898 | { |
||
1899 | if (null !== $this->collSubscriptions && !$overrideExisting) { |
||
1900 | return; |
||
1901 | } |
||
1902 | |||
1903 | $collectionClassName = SubscriptionTableMap::getTableMap()->getCollectionClassName(); |
||
1904 | |||
1905 | $this->collSubscriptions = new $collectionClassName; |
||
1906 | $this->collSubscriptions->setModel('\Jalle19\StatusManager\Database\Subscription'); |
||
1907 | } |
||
1908 | |||
1909 | /** |
||
1910 | * Gets an array of ChildSubscription objects which contain a foreign key that references this object. |
||
1911 | * |
||
1912 | * If the $criteria is not null, it is used to always fetch the results from the database. |
||
1913 | * Otherwise the results are fetched from the database the first time, then cached. |
||
1914 | * Next time the same method is called without $criteria, the cached collection is returned. |
||
1915 | * If this ChildInstance is new, it will return |
||
1916 | * an empty collection or the current collection; the criteria is ignored on a new object. |
||
1917 | * |
||
1918 | * @param Criteria $criteria optional Criteria object to narrow the query |
||
1919 | * @param ConnectionInterface $con optional connection object |
||
1920 | * @return ObjectCollection|ChildSubscription[] List of ChildSubscription objects |
||
1921 | * @throws PropelException |
||
1922 | */ |
||
1923 | public function getSubscriptions(Criteria $criteria = null, ConnectionInterface $con = null) |
||
1924 | { |
||
1925 | $partial = $this->collSubscriptionsPartial && !$this->isNew(); |
||
1926 | if (null === $this->collSubscriptions || null !== $criteria || $partial) { |
||
1927 | if ($this->isNew() && null === $this->collSubscriptions) { |
||
1928 | // return empty collection |
||
1929 | $this->initSubscriptions(); |
||
1930 | } else { |
||
1931 | $collSubscriptions = ChildSubscriptionQuery::create(null, $criteria) |
||
1932 | ->filterByInstance($this) |
||
1933 | ->find($con); |
||
1934 | |||
1935 | if (null !== $criteria) { |
||
1936 | if (false !== $this->collSubscriptionsPartial && count($collSubscriptions)) { |
||
1937 | $this->initSubscriptions(false); |
||
1938 | |||
1939 | foreach ($collSubscriptions as $obj) { |
||
1940 | if (false == $this->collSubscriptions->contains($obj)) { |
||
1941 | $this->collSubscriptions->append($obj); |
||
1942 | } |
||
1943 | } |
||
1944 | |||
1945 | $this->collSubscriptionsPartial = true; |
||
1946 | } |
||
1947 | |||
1948 | return $collSubscriptions; |
||
1949 | } |
||
1950 | |||
1951 | if ($partial && $this->collSubscriptions) { |
||
1952 | foreach ($this->collSubscriptions as $obj) { |
||
1953 | if ($obj->isNew()) { |
||
1954 | $collSubscriptions[] = $obj; |
||
1955 | } |
||
1956 | } |
||
1957 | } |
||
1958 | |||
1959 | $this->collSubscriptions = $collSubscriptions; |
||
1960 | $this->collSubscriptionsPartial = false; |
||
1961 | } |
||
1962 | } |
||
1963 | |||
1964 | return $this->collSubscriptions; |
||
1965 | } |
||
1966 | |||
1967 | /** |
||
1968 | * Sets a collection of ChildSubscription objects related by a one-to-many relationship |
||
1969 | * to the current object. |
||
1970 | * It will also schedule objects for deletion based on a diff between old objects (aka persisted) |
||
1971 | * and new objects from the given Propel collection. |
||
1972 | * |
||
1973 | * @param Collection $subscriptions A Propel collection. |
||
1974 | * @param ConnectionInterface $con Optional connection object |
||
1975 | * @return $this|ChildInstance The current object (for fluent API support) |
||
1976 | */ |
||
1977 | public function setSubscriptions(Collection $subscriptions, ConnectionInterface $con = null) |
||
1978 | { |
||
1979 | /** @var ChildSubscription[] $subscriptionsToDelete */ |
||
1980 | $subscriptionsToDelete = $this->getSubscriptions(new Criteria(), $con)->diff($subscriptions); |
||
1981 | |||
1982 | |||
1983 | $this->subscriptionsScheduledForDeletion = $subscriptionsToDelete; |
||
1984 | |||
1985 | foreach ($subscriptionsToDelete as $subscriptionRemoved) { |
||
1986 | $subscriptionRemoved->setInstance(null); |
||
1987 | } |
||
1988 | |||
1989 | $this->collSubscriptions = null; |
||
1990 | foreach ($subscriptions as $subscription) { |
||
1991 | $this->addSubscription($subscription); |
||
1992 | } |
||
1993 | |||
1994 | $this->collSubscriptions = $subscriptions; |
||
1995 | $this->collSubscriptionsPartial = false; |
||
1996 | |||
1997 | return $this; |
||
1998 | } |
||
1999 | |||
2000 | /** |
||
2001 | * Returns the number of related Subscription objects. |
||
2002 | * |
||
2003 | * @param Criteria $criteria |
||
2004 | * @param boolean $distinct |
||
2005 | * @param ConnectionInterface $con |
||
2006 | * @return int Count of related Subscription objects. |
||
2007 | * @throws PropelException |
||
2008 | */ |
||
2009 | public function countSubscriptions(Criteria $criteria = null, $distinct = false, ConnectionInterface $con = null) |
||
2010 | { |
||
2011 | $partial = $this->collSubscriptionsPartial && !$this->isNew(); |
||
2012 | if (null === $this->collSubscriptions || null !== $criteria || $partial) { |
||
2013 | if ($this->isNew() && null === $this->collSubscriptions) { |
||
2014 | return 0; |
||
2015 | } |
||
2016 | |||
2017 | if ($partial && !$criteria) { |
||
2018 | return count($this->getSubscriptions()); |
||
2019 | } |
||
2020 | |||
2021 | $query = ChildSubscriptionQuery::create(null, $criteria); |
||
2022 | if ($distinct) { |
||
2023 | $query->distinct(); |
||
2024 | } |
||
2025 | |||
2026 | return $query |
||
2027 | ->filterByInstance($this) |
||
2028 | ->count($con); |
||
2029 | } |
||
2030 | |||
2031 | return count($this->collSubscriptions); |
||
2032 | } |
||
2033 | |||
2034 | /** |
||
2035 | * Method called to associate a ChildSubscription object to this object |
||
2036 | * through the ChildSubscription foreign key attribute. |
||
2037 | * |
||
2038 | * @param ChildSubscription $l ChildSubscription |
||
2039 | * @return $this|\Jalle19\StatusManager\Database\Instance The current object (for fluent API support) |
||
2040 | */ |
||
2041 | public function addSubscription(ChildSubscription $l) |
||
2042 | { |
||
2043 | if ($this->collSubscriptions === null) { |
||
2044 | $this->initSubscriptions(); |
||
2045 | $this->collSubscriptionsPartial = true; |
||
2046 | } |
||
2047 | |||
2048 | if (!$this->collSubscriptions->contains($l)) { |
||
2049 | $this->doAddSubscription($l); |
||
2050 | |||
2051 | if ($this->subscriptionsScheduledForDeletion and $this->subscriptionsScheduledForDeletion->contains($l)) { |
||
2052 | $this->subscriptionsScheduledForDeletion->remove($this->subscriptionsScheduledForDeletion->search($l)); |
||
2053 | } |
||
2054 | } |
||
2055 | |||
2056 | return $this; |
||
2057 | } |
||
2058 | |||
2059 | /** |
||
2060 | * @param ChildSubscription $subscription The ChildSubscription object to add. |
||
2061 | */ |
||
2062 | protected function doAddSubscription(ChildSubscription $subscription) |
||
2063 | { |
||
2064 | $this->collSubscriptions[]= $subscription; |
||
2065 | $subscription->setInstance($this); |
||
2066 | } |
||
2067 | |||
2068 | /** |
||
2069 | * @param ChildSubscription $subscription The ChildSubscription object to remove. |
||
2070 | * @return $this|ChildInstance The current object (for fluent API support) |
||
2071 | */ |
||
2072 | public function removeSubscription(ChildSubscription $subscription) |
||
2073 | { |
||
2074 | if ($this->getSubscriptions()->contains($subscription)) { |
||
2075 | $pos = $this->collSubscriptions->search($subscription); |
||
2076 | $this->collSubscriptions->remove($pos); |
||
2077 | if (null === $this->subscriptionsScheduledForDeletion) { |
||
2078 | $this->subscriptionsScheduledForDeletion = clone $this->collSubscriptions; |
||
2079 | $this->subscriptionsScheduledForDeletion->clear(); |
||
2080 | } |
||
2081 | $this->subscriptionsScheduledForDeletion[]= clone $subscription; |
||
2082 | $subscription->setInstance(null); |
||
2083 | } |
||
2084 | |||
2085 | return $this; |
||
2086 | } |
||
2087 | |||
2088 | |||
2089 | /** |
||
2090 | * If this collection has already been initialized with |
||
2091 | * an identical criteria, it returns the collection. |
||
2092 | * Otherwise if this Instance is new, it will return |
||
2093 | * an empty collection; or if this Instance has previously |
||
2094 | * been saved, it will retrieve related Subscriptions from storage. |
||
2095 | * |
||
2096 | * This method is protected by default in order to keep the public |
||
2097 | * api reasonable. You can provide public methods for those you |
||
2098 | * actually need in Instance. |
||
2099 | * |
||
2100 | * @param Criteria $criteria optional Criteria object to narrow the query |
||
2101 | * @param ConnectionInterface $con optional connection object |
||
2102 | * @param string $joinBehavior optional join type to use (defaults to Criteria::LEFT_JOIN) |
||
2103 | * @return ObjectCollection|ChildSubscription[] List of ChildSubscription objects |
||
2104 | */ |
||
2105 | public function getSubscriptionsJoinUser(Criteria $criteria = null, ConnectionInterface $con = null, $joinBehavior = Criteria::LEFT_JOIN) |
||
2106 | { |
||
2107 | $query = ChildSubscriptionQuery::create(null, $criteria); |
||
2108 | $query->joinWith('User', $joinBehavior); |
||
2109 | |||
2110 | return $this->getSubscriptions($query, $con); |
||
2111 | } |
||
2112 | |||
2113 | |||
2114 | /** |
||
2115 | * If this collection has already been initialized with |
||
2116 | * an identical criteria, it returns the collection. |
||
2117 | * Otherwise if this Instance is new, it will return |
||
2118 | * an empty collection; or if this Instance has previously |
||
2119 | * been saved, it will retrieve related Subscriptions from storage. |
||
2120 | * |
||
2121 | * This method is protected by default in order to keep the public |
||
2122 | * api reasonable. You can provide public methods for those you |
||
2123 | * actually need in Instance. |
||
2124 | * |
||
2125 | * @param Criteria $criteria optional Criteria object to narrow the query |
||
2126 | * @param ConnectionInterface $con optional connection object |
||
2127 | * @param string $joinBehavior optional join type to use (defaults to Criteria::LEFT_JOIN) |
||
2128 | * @return ObjectCollection|ChildSubscription[] List of ChildSubscription objects |
||
2129 | */ |
||
2130 | public function getSubscriptionsJoinChannel(Criteria $criteria = null, ConnectionInterface $con = null, $joinBehavior = Criteria::LEFT_JOIN) |
||
2131 | { |
||
2132 | $query = ChildSubscriptionQuery::create(null, $criteria); |
||
2133 | $query->joinWith('Channel', $joinBehavior); |
||
2134 | |||
2135 | return $this->getSubscriptions($query, $con); |
||
2136 | } |
||
2137 | |||
2138 | /** |
||
2139 | * Clears the current object, sets all attributes to their default values and removes |
||
2140 | * outgoing references as well as back-references (from other objects to this one. Results probably in a database |
||
2141 | * change of those foreign objects when you call `save` there). |
||
2142 | */ |
||
2143 | public function clear() |
||
2144 | { |
||
2145 | $this->name = null; |
||
2146 | $this->alreadyInSave = false; |
||
2147 | $this->clearAllReferences(); |
||
2148 | $this->resetModified(); |
||
2149 | $this->setNew(true); |
||
2150 | $this->setDeleted(false); |
||
2151 | } |
||
2152 | |||
2153 | /** |
||
2154 | * Resets all references and back-references to other model objects or collections of model objects. |
||
2155 | * |
||
2156 | * This method is used to reset all php object references (not the actual reference in the database). |
||
2157 | * Necessary for object serialisation. |
||
2158 | * |
||
2159 | * @param boolean $deep Whether to also clear the references on all referrer objects. |
||
2160 | */ |
||
2161 | public function clearAllReferences($deep = false) |
||
2162 | { |
||
2163 | if ($deep) { |
||
2164 | if ($this->collUsers) { |
||
2165 | foreach ($this->collUsers as $o) { |
||
2166 | $o->clearAllReferences($deep); |
||
2167 | } |
||
2168 | } |
||
2169 | if ($this->collConnections) { |
||
2170 | foreach ($this->collConnections as $o) { |
||
2171 | $o->clearAllReferences($deep); |
||
2172 | } |
||
2173 | } |
||
2174 | if ($this->collChannels) { |
||
2175 | foreach ($this->collChannels as $o) { |
||
2176 | $o->clearAllReferences($deep); |
||
2177 | } |
||
2178 | } |
||
2179 | if ($this->collSubscriptions) { |
||
2180 | foreach ($this->collSubscriptions as $o) { |
||
2181 | $o->clearAllReferences($deep); |
||
2182 | } |
||
2183 | } |
||
2184 | } // if ($deep) |
||
2185 | |||
2186 | $this->collUsers = null; |
||
2187 | $this->collConnections = null; |
||
2188 | $this->collChannels = null; |
||
2189 | $this->collSubscriptions = null; |
||
2190 | } |
||
2191 | |||
2192 | /** |
||
2193 | * Return the string representation of this object |
||
2194 | * |
||
2195 | * @return string |
||
2196 | */ |
||
2197 | public function __toString() |
||
2198 | { |
||
2199 | return (string) $this->exportTo(InstanceTableMap::DEFAULT_STRING_FORMAT); |
||
2200 | } |
||
2201 | |||
2202 | /** |
||
2203 | * Code to be run before persisting the object |
||
2204 | * @param ConnectionInterface $con |
||
2205 | * @return boolean |
||
2206 | */ |
||
2207 | public function preSave(ConnectionInterface $con = null) |
||
2208 | { |
||
2209 | return true; |
||
2210 | } |
||
2211 | |||
2212 | /** |
||
2213 | * Code to be run after persisting the object |
||
2214 | * @param ConnectionInterface $con |
||
2215 | */ |
||
2216 | public function postSave(ConnectionInterface $con = null) |
||
2217 | { |
||
2218 | |||
2219 | } |
||
2220 | |||
2221 | /** |
||
2222 | * Code to be run before inserting to database |
||
2223 | * @param ConnectionInterface $con |
||
2224 | * @return boolean |
||
2225 | */ |
||
2226 | public function preInsert(ConnectionInterface $con = null) |
||
2227 | { |
||
2228 | return true; |
||
2229 | } |
||
2230 | |||
2231 | /** |
||
2232 | * Code to be run after inserting to database |
||
2233 | * @param ConnectionInterface $con |
||
2234 | */ |
||
2235 | public function postInsert(ConnectionInterface $con = null) |
||
2236 | { |
||
2237 | |||
2238 | } |
||
2239 | |||
2240 | /** |
||
2241 | * Code to be run before updating the object in database |
||
2242 | * @param ConnectionInterface $con |
||
2243 | * @return boolean |
||
2244 | */ |
||
2245 | public function preUpdate(ConnectionInterface $con = null) |
||
2246 | { |
||
2247 | return true; |
||
2248 | } |
||
2249 | |||
2250 | /** |
||
2251 | * Code to be run after updating the object in database |
||
2252 | * @param ConnectionInterface $con |
||
2253 | */ |
||
2254 | public function postUpdate(ConnectionInterface $con = null) |
||
2255 | { |
||
2256 | |||
2257 | } |
||
2258 | |||
2259 | /** |
||
2260 | * Code to be run before deleting the object in database |
||
2261 | * @param ConnectionInterface $con |
||
2262 | * @return boolean |
||
2263 | */ |
||
2264 | public function preDelete(ConnectionInterface $con = null) |
||
2265 | { |
||
2266 | return true; |
||
2267 | } |
||
2268 | |||
2269 | /** |
||
2270 | * Code to be run after deleting the object in database |
||
2271 | * @param ConnectionInterface $con |
||
2272 | */ |
||
2273 | public function postDelete(ConnectionInterface $con = null) |
||
2274 | { |
||
2275 | |||
2276 | } |
||
2277 | |||
2278 | |||
2279 | /** |
||
2280 | * Derived method to catches calls to undefined methods. |
||
2281 | * |
||
2282 | * Provides magic import/export method support (fromXML()/toXML(), fromYAML()/toYAML(), etc.). |
||
2283 | * Allows to define default __call() behavior if you overwrite __call() |
||
2284 | * |
||
2285 | * @param string $name |
||
2286 | * @param mixed $params |
||
2287 | * |
||
2288 | * @return array|string |
||
2289 | */ |
||
2290 | public function __call($name, $params) |
||
2291 | { |
||
2292 | if (0 === strpos($name, 'get')) { |
||
2293 | $virtualColumn = substr($name, 3); |
||
2294 | if ($this->hasVirtualColumn($virtualColumn)) { |
||
2295 | return $this->getVirtualColumn($virtualColumn); |
||
2296 | } |
||
2297 | |||
2298 | $virtualColumn = lcfirst($virtualColumn); |
||
2299 | if ($this->hasVirtualColumn($virtualColumn)) { |
||
2300 | return $this->getVirtualColumn($virtualColumn); |
||
2301 | } |
||
2302 | } |
||
2303 | |||
2304 | if (0 === strpos($name, 'from')) { |
||
2305 | $format = substr($name, 4); |
||
2306 | |||
2307 | return $this->importFrom($format, reset($params)); |
||
2308 | } |
||
2309 | |||
2310 | if (0 === strpos($name, 'to')) { |
||
2311 | $format = substr($name, 2); |
||
2312 | $includeLazyLoadColumns = isset($params[0]) ? $params[0] : true; |
||
2313 | |||
2314 | return $this->exportTo($format, $includeLazyLoadColumns); |
||
2315 | } |
||
2316 | |||
2317 | throw new BadMethodCallException(sprintf('Call to undefined method: %s.', $name)); |
||
2318 | } |
||
2319 | |||
2320 | } |
||
2321 |