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 InstanceQuery 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 InstanceQuery, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 92 | abstract class InstanceQuery extends ModelCriteria |
||
| 93 | { |
||
| 94 | protected $entityNotFoundExceptionClass = '\\Propel\\Runtime\\Exception\\EntityNotFoundException'; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Initializes internal state of \Jalle19\StatusManager\Database\Base\InstanceQuery object. |
||
| 98 | * |
||
| 99 | * @param string $dbName The database name |
||
| 100 | * @param string $modelName The phpName of a model, e.g. 'Book' |
||
| 101 | * @param string $modelAlias The alias for the model in this query, e.g. 'b' |
||
| 102 | */ |
||
| 103 | public function __construct($dbName = 'tvheadend_status_manager', $modelName = '\\Jalle19\\StatusManager\\Database\\Instance', $modelAlias = null) |
||
| 104 | { |
||
| 105 | parent::__construct($dbName, $modelName, $modelAlias); |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Returns a new ChildInstanceQuery object. |
||
| 110 | * |
||
| 111 | * @param string $modelAlias The alias of a model in the query |
||
| 112 | * @param Criteria $criteria Optional Criteria to build the query from |
||
| 113 | * |
||
| 114 | * @return ChildInstanceQuery |
||
| 115 | */ |
||
| 116 | public static function create($modelAlias = null, Criteria $criteria = null) |
||
| 117 | { |
||
| 118 | if ($criteria instanceof ChildInstanceQuery) { |
||
| 119 | return $criteria; |
||
| 120 | } |
||
| 121 | $query = new ChildInstanceQuery(); |
||
| 122 | if (null !== $modelAlias) { |
||
| 123 | $query->setModelAlias($modelAlias); |
||
| 124 | } |
||
| 125 | if ($criteria instanceof Criteria) { |
||
| 126 | $query->mergeWith($criteria); |
||
| 127 | } |
||
| 128 | |||
| 129 | return $query; |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Find object by primary key. |
||
| 134 | * Propel uses the instance pool to skip the database if the object exists. |
||
| 135 | * Go fast if the query is untouched. |
||
| 136 | * |
||
| 137 | * <code> |
||
| 138 | * $obj = $c->findPk(12, $con); |
||
| 139 | * </code> |
||
| 140 | * |
||
| 141 | * @param mixed $key Primary key to use for the query |
||
| 142 | * @param ConnectionInterface $con an optional connection object |
||
| 143 | * |
||
| 144 | * @return ChildInstance|array|mixed the result, formatted by the current formatter |
||
| 145 | */ |
||
| 146 | public function findPk($key, ConnectionInterface $con = null) |
||
| 147 | { |
||
| 148 | if ($key === null) { |
||
| 149 | return null; |
||
| 150 | } |
||
| 151 | if ((null !== ($obj = InstanceTableMap::getInstanceFromPool(null === $key || is_scalar($key) || is_callable([$key, '__toString']) ? (string) $key : $key))) && !$this->formatter) { |
||
| 152 | // the object is already in the instance pool |
||
| 153 | return $obj; |
||
| 154 | } |
||
| 155 | if ($con === null) { |
||
| 156 | $con = Propel::getServiceContainer()->getReadConnection(InstanceTableMap::DATABASE_NAME); |
||
| 157 | } |
||
| 158 | $this->basePreSelect($con); |
||
| 159 | if ($this->formatter || $this->modelAlias || $this->with || $this->select |
||
| 160 | || $this->selectColumns || $this->asColumns || $this->selectModifiers |
||
| 161 | || $this->map || $this->having || $this->joins) { |
||
| 162 | return $this->findPkComplex($key, $con); |
||
| 163 | } else { |
||
| 164 | return $this->findPkSimple($key, $con); |
||
| 165 | } |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Find object by primary key using raw SQL to go fast. |
||
| 170 | * Bypass doSelect() and the object formatter by using generated code. |
||
| 171 | * |
||
| 172 | * @param mixed $key Primary key to use for the query |
||
| 173 | * @param ConnectionInterface $con A connection object |
||
| 174 | * |
||
| 175 | * @throws \Propel\Runtime\Exception\PropelException |
||
| 176 | * |
||
| 177 | * @return ChildInstance A model object, or null if the key is not found |
||
| 178 | */ |
||
| 179 | protected function findPkSimple($key, ConnectionInterface $con) |
||
| 180 | { |
||
| 181 | $sql = 'SELECT name FROM instance WHERE name = :p0'; |
||
| 182 | try { |
||
| 183 | $stmt = $con->prepare($sql); |
||
| 184 | $stmt->bindValue(':p0', $key, PDO::PARAM_STR); |
||
| 185 | $stmt->execute(); |
||
| 186 | } catch (Exception $e) { |
||
| 187 | Propel::log($e->getMessage(), Propel::LOG_ERR); |
||
| 188 | throw new PropelException(sprintf('Unable to execute SELECT statement [%s]', $sql), 0, $e); |
||
| 189 | } |
||
| 190 | $obj = null; |
||
| 191 | if ($row = $stmt->fetch(\PDO::FETCH_NUM)) { |
||
| 192 | /** @var ChildInstance $obj */ |
||
| 193 | $obj = new ChildInstance(); |
||
| 194 | $obj->hydrate($row); |
||
| 195 | InstanceTableMap::addInstanceToPool($obj, null === $key || is_scalar($key) || is_callable([$key, '__toString']) ? (string) $key : $key); |
||
| 196 | } |
||
| 197 | $stmt->closeCursor(); |
||
| 198 | |||
| 199 | return $obj; |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Find object by primary key. |
||
| 204 | * |
||
| 205 | * @param mixed $key Primary key to use for the query |
||
| 206 | * @param ConnectionInterface $con A connection object |
||
| 207 | * |
||
| 208 | * @return ChildInstance|array|mixed the result, formatted by the current formatter |
||
| 209 | */ |
||
| 210 | protected function findPkComplex($key, ConnectionInterface $con) |
||
| 211 | { |
||
| 212 | // As the query uses a PK condition, no limit(1) is necessary. |
||
| 213 | $criteria = $this->isKeepQuery() ? clone $this : $this; |
||
| 214 | $dataFetcher = $criteria |
||
| 215 | ->filterByPrimaryKey($key) |
||
| 216 | ->doSelect($con); |
||
| 217 | |||
| 218 | return $criteria->getFormatter()->init($criteria)->formatOne($dataFetcher); |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Find objects by primary key |
||
| 223 | * <code> |
||
| 224 | * $objs = $c->findPks(array(12, 56, 832), $con); |
||
| 225 | * </code> |
||
| 226 | * @param array $keys Primary keys to use for the query |
||
| 227 | * @param ConnectionInterface $con an optional connection object |
||
| 228 | * |
||
| 229 | * @return ObjectCollection|array|mixed the list of results, formatted by the current formatter |
||
| 230 | */ |
||
| 231 | public function findPks($keys, ConnectionInterface $con = null) |
||
| 232 | { |
||
| 233 | if (null === $con) { |
||
| 234 | $con = Propel::getServiceContainer()->getReadConnection($this->getDbName()); |
||
| 235 | } |
||
| 236 | $this->basePreSelect($con); |
||
| 237 | $criteria = $this->isKeepQuery() ? clone $this : $this; |
||
| 238 | $dataFetcher = $criteria |
||
| 239 | ->filterByPrimaryKeys($keys) |
||
| 240 | ->doSelect($con); |
||
| 241 | |||
| 242 | return $criteria->getFormatter()->init($criteria)->format($dataFetcher); |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Filter the query by primary key |
||
| 247 | * |
||
| 248 | * @param mixed $key Primary key to use for the query |
||
| 249 | * |
||
| 250 | * @return $this|ChildInstanceQuery The current query, for fluid interface |
||
| 251 | */ |
||
| 252 | public function filterByPrimaryKey($key) |
||
| 253 | { |
||
| 254 | |||
| 255 | return $this->addUsingAlias(InstanceTableMap::COL_NAME, $key, Criteria::EQUAL); |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Filter the query by a list of primary keys |
||
| 260 | * |
||
| 261 | * @param array $keys The list of primary key to use for the query |
||
| 262 | * |
||
| 263 | * @return $this|ChildInstanceQuery The current query, for fluid interface |
||
| 264 | */ |
||
| 265 | public function filterByPrimaryKeys($keys) |
||
| 266 | { |
||
| 267 | |||
| 268 | return $this->addUsingAlias(InstanceTableMap::COL_NAME, $keys, Criteria::IN); |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Filter the query on the name column |
||
| 273 | * |
||
| 274 | * Example usage: |
||
| 275 | * <code> |
||
| 276 | * $query->filterByName('fooValue'); // WHERE name = 'fooValue' |
||
| 277 | * $query->filterByName('%fooValue%'); // WHERE name LIKE '%fooValue%' |
||
| 278 | * </code> |
||
| 279 | * |
||
| 280 | * @param string $name The value to use as filter. |
||
| 281 | * Accepts wildcards (* and % trigger a LIKE) |
||
| 282 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
| 283 | * |
||
| 284 | * @return $this|ChildInstanceQuery The current query, for fluid interface |
||
| 285 | */ |
||
| 286 | public function filterByName($name = null, $comparison = null) |
||
| 287 | { |
||
| 288 | if (null === $comparison) { |
||
| 289 | if (is_array($name)) { |
||
| 290 | $comparison = Criteria::IN; |
||
| 291 | } elseif (preg_match('/[\%\*]/', $name)) { |
||
| 292 | $name = str_replace('*', '%', $name); |
||
| 293 | $comparison = Criteria::LIKE; |
||
| 294 | } |
||
| 295 | } |
||
| 296 | |||
| 297 | return $this->addUsingAlias(InstanceTableMap::COL_NAME, $name, $comparison); |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Filter the query by a related \Jalle19\StatusManager\Database\User object |
||
| 302 | * |
||
| 303 | * @param \Jalle19\StatusManager\Database\User|ObjectCollection $user the related object to use as filter |
||
| 304 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
| 305 | * |
||
| 306 | * @return ChildInstanceQuery The current query, for fluid interface |
||
| 307 | */ |
||
| 308 | public function filterByUser($user, $comparison = null) |
||
| 309 | { |
||
| 310 | if ($user instanceof \Jalle19\StatusManager\Database\User) { |
||
| 311 | return $this |
||
| 312 | ->addUsingAlias(InstanceTableMap::COL_NAME, $user->getInstanceName(), $comparison); |
||
| 313 | } elseif ($user instanceof ObjectCollection) { |
||
| 314 | return $this |
||
| 315 | ->useUserQuery() |
||
| 316 | ->filterByPrimaryKeys($user->getPrimaryKeys()) |
||
| 317 | ->endUse(); |
||
| 318 | } else { |
||
| 319 | throw new PropelException('filterByUser() only accepts arguments of type \Jalle19\StatusManager\Database\User or Collection'); |
||
| 320 | } |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Adds a JOIN clause to the query using the User relation |
||
| 325 | * |
||
| 326 | * @param string $relationAlias optional alias for the relation |
||
| 327 | * @param string $joinType Accepted values are null, 'left join', 'right join', 'inner join' |
||
| 328 | * |
||
| 329 | * @return $this|ChildInstanceQuery The current query, for fluid interface |
||
| 330 | */ |
||
| 331 | public function joinUser($relationAlias = null, $joinType = Criteria::INNER_JOIN) |
||
| 332 | { |
||
| 333 | $tableMap = $this->getTableMap(); |
||
| 334 | $relationMap = $tableMap->getRelation('User'); |
||
| 335 | |||
| 336 | // create a ModelJoin object for this join |
||
| 337 | $join = new ModelJoin(); |
||
| 338 | $join->setJoinType($joinType); |
||
| 339 | $join->setRelationMap($relationMap, $this->useAliasInSQL ? $this->getModelAlias() : null, $relationAlias); |
||
| 340 | if ($previousJoin = $this->getPreviousJoin()) { |
||
| 341 | $join->setPreviousJoin($previousJoin); |
||
| 342 | } |
||
| 343 | |||
| 344 | // add the ModelJoin to the current object |
||
| 345 | if ($relationAlias) { |
||
| 346 | $this->addAlias($relationAlias, $relationMap->getRightTable()->getName()); |
||
| 347 | $this->addJoinObject($join, $relationAlias); |
||
| 348 | } else { |
||
| 349 | $this->addJoinObject($join, 'User'); |
||
| 350 | } |
||
| 351 | |||
| 352 | return $this; |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Use the User relation User object |
||
| 357 | * |
||
| 358 | * @see useQuery() |
||
| 359 | * |
||
| 360 | * @param string $relationAlias optional alias for the relation, |
||
| 361 | * to be used as main alias in the secondary query |
||
| 362 | * @param string $joinType Accepted values are null, 'left join', 'right join', 'inner join' |
||
| 363 | * |
||
| 364 | * @return \Jalle19\StatusManager\Database\UserQuery A secondary query class using the current class as primary query |
||
| 365 | */ |
||
| 366 | public function useUserQuery($relationAlias = null, $joinType = Criteria::INNER_JOIN) |
||
| 367 | { |
||
| 368 | return $this |
||
| 369 | ->joinUser($relationAlias, $joinType) |
||
| 370 | ->useQuery($relationAlias ? $relationAlias : 'User', '\Jalle19\StatusManager\Database\UserQuery'); |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Filter the query by a related \Jalle19\StatusManager\Database\Connection object |
||
| 375 | * |
||
| 376 | * @param \Jalle19\StatusManager\Database\Connection|ObjectCollection $connection the related object to use as filter |
||
| 377 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
| 378 | * |
||
| 379 | * @return ChildInstanceQuery The current query, for fluid interface |
||
| 380 | */ |
||
| 381 | public function filterByConnection($connection, $comparison = null) |
||
| 382 | { |
||
| 383 | if ($connection instanceof \Jalle19\StatusManager\Database\Connection) { |
||
| 384 | return $this |
||
| 385 | ->addUsingAlias(InstanceTableMap::COL_NAME, $connection->getInstanceName(), $comparison); |
||
| 386 | } elseif ($connection instanceof ObjectCollection) { |
||
| 387 | return $this |
||
| 388 | ->useConnectionQuery() |
||
| 389 | ->filterByPrimaryKeys($connection->getPrimaryKeys()) |
||
| 390 | ->endUse(); |
||
| 391 | } else { |
||
| 392 | throw new PropelException('filterByConnection() only accepts arguments of type \Jalle19\StatusManager\Database\Connection or Collection'); |
||
| 393 | } |
||
| 394 | } |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Adds a JOIN clause to the query using the Connection relation |
||
| 398 | * |
||
| 399 | * @param string $relationAlias optional alias for the relation |
||
| 400 | * @param string $joinType Accepted values are null, 'left join', 'right join', 'inner join' |
||
| 401 | * |
||
| 402 | * @return $this|ChildInstanceQuery The current query, for fluid interface |
||
| 403 | */ |
||
| 404 | public function joinConnection($relationAlias = null, $joinType = Criteria::INNER_JOIN) |
||
| 405 | { |
||
| 406 | $tableMap = $this->getTableMap(); |
||
| 407 | $relationMap = $tableMap->getRelation('Connection'); |
||
| 408 | |||
| 409 | // create a ModelJoin object for this join |
||
| 410 | $join = new ModelJoin(); |
||
| 411 | $join->setJoinType($joinType); |
||
| 412 | $join->setRelationMap($relationMap, $this->useAliasInSQL ? $this->getModelAlias() : null, $relationAlias); |
||
| 413 | if ($previousJoin = $this->getPreviousJoin()) { |
||
| 414 | $join->setPreviousJoin($previousJoin); |
||
| 415 | } |
||
| 416 | |||
| 417 | // add the ModelJoin to the current object |
||
| 418 | if ($relationAlias) { |
||
| 419 | $this->addAlias($relationAlias, $relationMap->getRightTable()->getName()); |
||
| 420 | $this->addJoinObject($join, $relationAlias); |
||
| 421 | } else { |
||
| 422 | $this->addJoinObject($join, 'Connection'); |
||
| 423 | } |
||
| 424 | |||
| 425 | return $this; |
||
| 426 | } |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Use the Connection relation Connection object |
||
| 430 | * |
||
| 431 | * @see useQuery() |
||
| 432 | * |
||
| 433 | * @param string $relationAlias optional alias for the relation, |
||
| 434 | * to be used as main alias in the secondary query |
||
| 435 | * @param string $joinType Accepted values are null, 'left join', 'right join', 'inner join' |
||
| 436 | * |
||
| 437 | * @return \Jalle19\StatusManager\Database\ConnectionQuery A secondary query class using the current class as primary query |
||
| 438 | */ |
||
| 439 | public function useConnectionQuery($relationAlias = null, $joinType = Criteria::INNER_JOIN) |
||
| 440 | { |
||
| 441 | return $this |
||
| 442 | ->joinConnection($relationAlias, $joinType) |
||
| 443 | ->useQuery($relationAlias ? $relationAlias : 'Connection', '\Jalle19\StatusManager\Database\ConnectionQuery'); |
||
| 444 | } |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Filter the query by a related \Jalle19\StatusManager\Database\Channel object |
||
| 448 | * |
||
| 449 | * @param \Jalle19\StatusManager\Database\Channel|ObjectCollection $channel the related object to use as filter |
||
| 450 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
| 451 | * |
||
| 452 | * @return ChildInstanceQuery The current query, for fluid interface |
||
| 453 | */ |
||
| 454 | public function filterByChannel($channel, $comparison = null) |
||
| 455 | { |
||
| 456 | if ($channel instanceof \Jalle19\StatusManager\Database\Channel) { |
||
| 457 | return $this |
||
| 458 | ->addUsingAlias(InstanceTableMap::COL_NAME, $channel->getInstanceName(), $comparison); |
||
| 459 | } elseif ($channel instanceof ObjectCollection) { |
||
| 460 | return $this |
||
| 461 | ->useChannelQuery() |
||
| 462 | ->filterByPrimaryKeys($channel->getPrimaryKeys()) |
||
| 463 | ->endUse(); |
||
| 464 | } else { |
||
| 465 | throw new PropelException('filterByChannel() only accepts arguments of type \Jalle19\StatusManager\Database\Channel or Collection'); |
||
| 466 | } |
||
| 467 | } |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Adds a JOIN clause to the query using the Channel relation |
||
| 471 | * |
||
| 472 | * @param string $relationAlias optional alias for the relation |
||
| 473 | * @param string $joinType Accepted values are null, 'left join', 'right join', 'inner join' |
||
| 474 | * |
||
| 475 | * @return $this|ChildInstanceQuery The current query, for fluid interface |
||
| 476 | */ |
||
| 477 | public function joinChannel($relationAlias = null, $joinType = Criteria::INNER_JOIN) |
||
| 478 | { |
||
| 479 | $tableMap = $this->getTableMap(); |
||
| 480 | $relationMap = $tableMap->getRelation('Channel'); |
||
| 481 | |||
| 482 | // create a ModelJoin object for this join |
||
| 483 | $join = new ModelJoin(); |
||
| 484 | $join->setJoinType($joinType); |
||
| 485 | $join->setRelationMap($relationMap, $this->useAliasInSQL ? $this->getModelAlias() : null, $relationAlias); |
||
| 486 | if ($previousJoin = $this->getPreviousJoin()) { |
||
| 487 | $join->setPreviousJoin($previousJoin); |
||
| 488 | } |
||
| 489 | |||
| 490 | // add the ModelJoin to the current object |
||
| 491 | if ($relationAlias) { |
||
| 492 | $this->addAlias($relationAlias, $relationMap->getRightTable()->getName()); |
||
| 493 | $this->addJoinObject($join, $relationAlias); |
||
| 494 | } else { |
||
| 495 | $this->addJoinObject($join, 'Channel'); |
||
| 496 | } |
||
| 497 | |||
| 498 | return $this; |
||
| 499 | } |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Use the Channel relation Channel object |
||
| 503 | * |
||
| 504 | * @see useQuery() |
||
| 505 | * |
||
| 506 | * @param string $relationAlias optional alias for the relation, |
||
| 507 | * to be used as main alias in the secondary query |
||
| 508 | * @param string $joinType Accepted values are null, 'left join', 'right join', 'inner join' |
||
| 509 | * |
||
| 510 | * @return \Jalle19\StatusManager\Database\ChannelQuery A secondary query class using the current class as primary query |
||
| 511 | */ |
||
| 512 | public function useChannelQuery($relationAlias = null, $joinType = Criteria::INNER_JOIN) |
||
| 513 | { |
||
| 514 | return $this |
||
| 515 | ->joinChannel($relationAlias, $joinType) |
||
| 516 | ->useQuery($relationAlias ? $relationAlias : 'Channel', '\Jalle19\StatusManager\Database\ChannelQuery'); |
||
| 517 | } |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Filter the query by a related \Jalle19\StatusManager\Database\Subscription object |
||
| 521 | * |
||
| 522 | * @param \Jalle19\StatusManager\Database\Subscription|ObjectCollection $subscription the related object to use as filter |
||
| 523 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
| 524 | * |
||
| 525 | * @return ChildInstanceQuery The current query, for fluid interface |
||
| 526 | */ |
||
| 527 | public function filterBySubscription($subscription, $comparison = null) |
||
| 528 | { |
||
| 529 | if ($subscription instanceof \Jalle19\StatusManager\Database\Subscription) { |
||
| 530 | return $this |
||
| 531 | ->addUsingAlias(InstanceTableMap::COL_NAME, $subscription->getInstanceName(), $comparison); |
||
| 532 | } elseif ($subscription instanceof ObjectCollection) { |
||
| 533 | return $this |
||
| 534 | ->useSubscriptionQuery() |
||
| 535 | ->filterByPrimaryKeys($subscription->getPrimaryKeys()) |
||
| 536 | ->endUse(); |
||
| 537 | } else { |
||
| 538 | throw new PropelException('filterBySubscription() only accepts arguments of type \Jalle19\StatusManager\Database\Subscription or Collection'); |
||
| 539 | } |
||
| 540 | } |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Adds a JOIN clause to the query using the Subscription relation |
||
| 544 | * |
||
| 545 | * @param string $relationAlias optional alias for the relation |
||
| 546 | * @param string $joinType Accepted values are null, 'left join', 'right join', 'inner join' |
||
| 547 | * |
||
| 548 | * @return $this|ChildInstanceQuery The current query, for fluid interface |
||
| 549 | */ |
||
| 550 | public function joinSubscription($relationAlias = null, $joinType = Criteria::INNER_JOIN) |
||
| 551 | { |
||
| 552 | $tableMap = $this->getTableMap(); |
||
| 553 | $relationMap = $tableMap->getRelation('Subscription'); |
||
| 554 | |||
| 555 | // create a ModelJoin object for this join |
||
| 556 | $join = new ModelJoin(); |
||
| 557 | $join->setJoinType($joinType); |
||
| 558 | $join->setRelationMap($relationMap, $this->useAliasInSQL ? $this->getModelAlias() : null, $relationAlias); |
||
| 559 | if ($previousJoin = $this->getPreviousJoin()) { |
||
| 560 | $join->setPreviousJoin($previousJoin); |
||
| 561 | } |
||
| 562 | |||
| 563 | // add the ModelJoin to the current object |
||
| 564 | if ($relationAlias) { |
||
| 565 | $this->addAlias($relationAlias, $relationMap->getRightTable()->getName()); |
||
| 566 | $this->addJoinObject($join, $relationAlias); |
||
| 567 | } else { |
||
| 568 | $this->addJoinObject($join, 'Subscription'); |
||
| 569 | } |
||
| 570 | |||
| 571 | return $this; |
||
| 572 | } |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Use the Subscription relation Subscription object |
||
| 576 | * |
||
| 577 | * @see useQuery() |
||
| 578 | * |
||
| 579 | * @param string $relationAlias optional alias for the relation, |
||
| 580 | * to be used as main alias in the secondary query |
||
| 581 | * @param string $joinType Accepted values are null, 'left join', 'right join', 'inner join' |
||
| 582 | * |
||
| 583 | * @return \Jalle19\StatusManager\Database\SubscriptionQuery A secondary query class using the current class as primary query |
||
| 584 | */ |
||
| 585 | public function useSubscriptionQuery($relationAlias = null, $joinType = Criteria::INNER_JOIN) |
||
| 586 | { |
||
| 587 | return $this |
||
| 588 | ->joinSubscription($relationAlias, $joinType) |
||
| 589 | ->useQuery($relationAlias ? $relationAlias : 'Subscription', '\Jalle19\StatusManager\Database\SubscriptionQuery'); |
||
| 590 | } |
||
| 591 | |||
| 592 | /** |
||
| 593 | * Exclude object from result |
||
| 594 | * |
||
| 595 | * @param ChildInstance $instance Object to remove from the list of results |
||
| 596 | * |
||
| 597 | * @return $this|ChildInstanceQuery The current query, for fluid interface |
||
| 598 | */ |
||
| 599 | public function prune($instance = null) |
||
| 600 | { |
||
| 601 | if ($instance) { |
||
| 602 | $this->addUsingAlias(InstanceTableMap::COL_NAME, $instance->getName(), Criteria::NOT_EQUAL); |
||
| 603 | } |
||
| 604 | |||
| 605 | return $this; |
||
| 606 | } |
||
| 607 | |||
| 608 | /** |
||
| 609 | * Deletes all rows from the instance table. |
||
| 610 | * |
||
| 611 | * @param ConnectionInterface $con the connection to use |
||
| 612 | * @return int The number of affected rows (if supported by underlying database driver). |
||
| 613 | */ |
||
| 614 | public function doDeleteAll(ConnectionInterface $con = null) |
||
| 615 | { |
||
| 616 | if (null === $con) { |
||
| 617 | $con = Propel::getServiceContainer()->getWriteConnection(InstanceTableMap::DATABASE_NAME); |
||
| 618 | } |
||
| 619 | |||
| 620 | // use transaction because $criteria could contain info |
||
| 621 | // for more than one table or we could emulating ON DELETE CASCADE, etc. |
||
| 622 | return $con->transaction(function () use ($con) { |
||
| 623 | $affectedRows = 0; // initialize var to track total num of affected rows |
||
| 624 | $affectedRows += parent::doDeleteAll($con); |
||
| 625 | // Because this db requires some delete cascade/set null emulation, we have to |
||
| 626 | // clear the cached instance *after* the emulation has happened (since |
||
| 627 | // instances get re-added by the select statement contained therein). |
||
| 628 | InstanceTableMap::clearInstancePool(); |
||
| 629 | InstanceTableMap::clearRelatedInstancePool(); |
||
| 630 | |||
| 631 | return $affectedRows; |
||
| 632 | }); |
||
| 633 | } |
||
| 634 | |||
| 635 | /** |
||
| 636 | * Performs a DELETE on the database based on the current ModelCriteria |
||
| 637 | * |
||
| 638 | * @param ConnectionInterface $con the connection to use |
||
| 639 | * @return int The number of affected rows (if supported by underlying database driver). This includes CASCADE-related rows |
||
| 640 | * if supported by native driver or if emulated using Propel. |
||
| 641 | * @throws PropelException Any exceptions caught during processing will be |
||
| 642 | * rethrown wrapped into a PropelException. |
||
| 643 | */ |
||
| 644 | public function delete(ConnectionInterface $con = null) |
||
| 645 | { |
||
| 646 | if (null === $con) { |
||
| 647 | $con = Propel::getServiceContainer()->getWriteConnection(InstanceTableMap::DATABASE_NAME); |
||
| 648 | } |
||
| 649 | |||
| 650 | $criteria = $this; |
||
| 651 | |||
| 652 | // Set the correct dbName |
||
| 653 | $criteria->setDbName(InstanceTableMap::DATABASE_NAME); |
||
| 654 | |||
| 655 | // use transaction because $criteria could contain info |
||
| 656 | // for more than one table or we could emulating ON DELETE CASCADE, etc. |
||
| 657 | return $con->transaction(function () use ($con, $criteria) { |
||
| 658 | $affectedRows = 0; // initialize var to track total num of affected rows |
||
| 659 | |||
| 660 | InstanceTableMap::removeInstanceFromPool($criteria); |
||
| 661 | |||
| 662 | $affectedRows += ModelCriteria::delete($con); |
||
| 663 | InstanceTableMap::clearRelatedInstancePool(); |
||
| 664 | |||
| 665 | return $affectedRows; |
||
| 666 | }); |
||
| 667 | } |
||
| 668 | |||
| 669 | } // InstanceQuery |
||
| 670 |