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