Completed
Branch master (a15a51)
by Sam
01:56
created

Channel   F

Complexity

Total Complexity 220

Size/Duplication

Total Lines 1649
Duplicated Lines 33.23 %

Coupling/Cohesion

Components 1
Dependencies 17

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 220
lcom 1
cbo 17
dl 548
loc 1649
rs 0.6314
c 2
b 0
f 0

How to fix   Duplicated Code    Complexity   

Duplicated Code

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 Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Channel 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 Channel, and based on these observations, apply Extract Interface, too.

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