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