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 BannerImageI18nQuery 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 BannerImageI18nQuery, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 87 | abstract class BannerImageI18nQuery extends ModelCriteria |
||
| 88 | { |
||
| 89 | protected $entityNotFoundExceptionClass = '\\Propel\\Runtime\\Exception\\EntityNotFoundException'; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Initializes internal state of \xbanners\models\Base\BannerImageI18nQuery object. |
||
| 93 | * |
||
| 94 | * @param string $dbName The database name |
||
| 95 | * @param string $modelName The phpName of a model, e.g. 'Book' |
||
| 96 | * @param string $modelAlias The alias for the model in this query, e.g. 'b' |
||
| 97 | */ |
||
| 98 | public function __construct($dbName = 'Shop', $modelName = '\\xbanners\\models\\BannerImageI18n', $modelAlias = null) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Returns a new ChildBannerImageI18nQuery object. |
||
| 105 | * |
||
| 106 | * @param string $modelAlias The alias of a model in the query |
||
| 107 | * @param Criteria $criteria Optional Criteria to build the query from |
||
| 108 | * |
||
| 109 | * @return ChildBannerImageI18nQuery |
||
| 110 | */ |
||
| 111 | View Code Duplication | public static function create($modelAlias = null, Criteria $criteria = null) |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Find object by primary key. |
||
| 129 | * Propel uses the instance pool to skip the database if the object exists. |
||
| 130 | * Go fast if the query is untouched. |
||
| 131 | * |
||
| 132 | * <code> |
||
| 133 | * $obj = $c->findPk(array(12, 34), $con); |
||
| 134 | * </code> |
||
| 135 | * |
||
| 136 | * @param array[$id, $locale] $key Primary key to use for the query |
||
| 137 | * @param ConnectionInterface $con an optional connection object |
||
| 138 | * |
||
| 139 | * @return ChildBannerImageI18n|array|mixed the result, formatted by the current formatter |
||
| 140 | */ |
||
| 141 | public function findPk($key, ConnectionInterface $con = null) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Find object by primary key using raw SQL to go fast. |
||
| 171 | * Bypass doSelect() and the object formatter by using generated code. |
||
| 172 | * |
||
| 173 | * @param mixed $key Primary key to use for the query |
||
| 174 | * @param ConnectionInterface $con A connection object |
||
| 175 | * |
||
| 176 | * @throws \Propel\Runtime\Exception\PropelException |
||
| 177 | * |
||
| 178 | * @return ChildBannerImageI18n A model object, or null if the key is not found |
||
| 179 | */ |
||
| 180 | protected function findPkSimple($key, ConnectionInterface $con) |
||
| 181 | { |
||
| 182 | $sql = 'SELECT id, locale, src, name, clicks, description FROM banner_image_i18n WHERE id = :p0 AND locale = :p1'; |
||
| 183 | try { |
||
| 184 | $stmt = $con->prepare($sql); |
||
| 185 | $stmt->bindValue(':p0', $key[0], PDO::PARAM_INT); |
||
| 186 | $stmt->bindValue(':p1', $key[1], PDO::PARAM_STR); |
||
| 187 | $stmt->execute(); |
||
| 188 | } catch (Exception $e) { |
||
| 189 | Propel::log($e->getMessage(), Propel::LOG_ERR); |
||
| 190 | throw new PropelException(sprintf('Unable to execute SELECT statement [%s]', $sql), 0, $e); |
||
| 191 | } |
||
| 192 | $obj = null; |
||
| 193 | if ($row = $stmt->fetch(\PDO::FETCH_NUM)) { |
||
| 194 | /** @var ChildBannerImageI18n $obj */ |
||
| 195 | $obj = new ChildBannerImageI18n(); |
||
| 196 | $obj->hydrate($row); |
||
| 197 | BannerImageI18nTableMap::addInstanceToPool($obj, serialize([(null === $key[0] || is_scalar($key[0]) || is_callable([$key[0], '__toString']) ? (string) $key[0] : $key[0]), (null === $key[1] || is_scalar($key[1]) || is_callable([$key[1], '__toString']) ? (string) $key[1] : $key[1])])); |
||
| 198 | } |
||
| 199 | $stmt->closeCursor(); |
||
| 200 | |||
| 201 | return $obj; |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Find object by primary key. |
||
| 206 | * |
||
| 207 | * @param mixed $key Primary key to use for the query |
||
| 208 | * @param ConnectionInterface $con A connection object |
||
| 209 | * |
||
| 210 | * @return ChildBannerImageI18n|array|mixed the result, formatted by the current formatter |
||
| 211 | */ |
||
| 212 | protected function findPkComplex($key, ConnectionInterface $con) |
||
| 213 | { |
||
| 214 | // As the query uses a PK condition, no limit(1) is necessary. |
||
| 215 | $criteria = $this->isKeepQuery() ? clone $this : $this; |
||
| 216 | $dataFetcher = $criteria |
||
| 217 | ->filterByPrimaryKey($key) |
||
| 218 | ->doSelect($con); |
||
| 219 | |||
| 220 | return $criteria->getFormatter()->init($criteria)->formatOne($dataFetcher); |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Find objects by primary key |
||
| 225 | * <code> |
||
| 226 | * $objs = $c->findPks(array(array(12, 56), array(832, 123), array(123, 456)), $con); |
||
| 227 | * </code> |
||
| 228 | * @param array $keys Primary keys to use for the query |
||
| 229 | * @param ConnectionInterface $con an optional connection object |
||
| 230 | * |
||
| 231 | * @return ObjectCollection|array|mixed the list of results, formatted by the current formatter |
||
| 232 | */ |
||
| 233 | public function findPks($keys, ConnectionInterface $con = null) |
||
| 234 | { |
||
| 235 | if (null === $con) { |
||
| 236 | $con = Propel::getServiceContainer()->getReadConnection($this->getDbName()); |
||
| 237 | } |
||
| 238 | $this->basePreSelect($con); |
||
| 239 | $criteria = $this->isKeepQuery() ? clone $this : $this; |
||
| 240 | $dataFetcher = $criteria |
||
| 241 | ->filterByPrimaryKeys($keys) |
||
| 242 | ->doSelect($con); |
||
| 243 | |||
| 244 | return $criteria->getFormatter()->init($criteria)->format($dataFetcher); |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Filter the query by primary key |
||
| 249 | * |
||
| 250 | * @param mixed $key Primary key to use for the query |
||
| 251 | * |
||
| 252 | * @return $this|ChildBannerImageI18nQuery The current query, for fluid interface |
||
| 253 | */ |
||
| 254 | public function filterByPrimaryKey($key) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Filter the query by a list of primary keys |
||
| 264 | * |
||
| 265 | * @param array $keys The list of primary key to use for the query |
||
| 266 | * |
||
| 267 | * @return $this|ChildBannerImageI18nQuery The current query, for fluid interface |
||
| 268 | */ |
||
| 269 | public function filterByPrimaryKeys($keys) |
||
| 270 | { |
||
| 271 | if (empty($keys)) { |
||
| 272 | return $this->add(null, '1<>1', Criteria::CUSTOM); |
||
| 273 | } |
||
| 274 | foreach ($keys as $key) { |
||
| 275 | $cton0 = $this->getNewCriterion(BannerImageI18nTableMap::COL_ID, $key[0], Criteria::EQUAL); |
||
| 276 | $cton1 = $this->getNewCriterion(BannerImageI18nTableMap::COL_LOCALE, $key[1], Criteria::EQUAL); |
||
| 277 | $cton0->addAnd($cton1); |
||
| 278 | $this->addOr($cton0); |
||
| 279 | } |
||
| 280 | |||
| 281 | return $this; |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Filter the query on the id column |
||
| 286 | * |
||
| 287 | * Example usage: |
||
| 288 | * <code> |
||
| 289 | * $query->filterById(1234); // WHERE id = 1234 |
||
| 290 | * $query->filterById(array(12, 34)); // WHERE id IN (12, 34) |
||
| 291 | * $query->filterById(array('min' => 12)); // WHERE id > 12 |
||
| 292 | * </code> |
||
| 293 | * |
||
| 294 | * @see filterByBannerImage() |
||
| 295 | * |
||
| 296 | * @param mixed $id The value to use as filter. |
||
| 297 | * Use scalar values for equality. |
||
| 298 | * Use array values for in_array() equivalent. |
||
| 299 | * Use associative array('min' => $minValue, 'max' => $maxValue) for intervals. |
||
| 300 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
| 301 | * |
||
| 302 | * @return $this|ChildBannerImageI18nQuery The current query, for fluid interface |
||
| 303 | */ |
||
| 304 | View Code Duplication | public function filterById($id = null, $comparison = null) |
|
| 326 | |||
| 327 | /** |
||
| 328 | * Filter the query on the locale column |
||
| 329 | * |
||
| 330 | * Example usage: |
||
| 331 | * <code> |
||
| 332 | * $query->filterByLocale('fooValue'); // WHERE locale = 'fooValue' |
||
| 333 | * $query->filterByLocale('%fooValue%'); // WHERE locale LIKE '%fooValue%' |
||
| 334 | * </code> |
||
| 335 | * |
||
| 336 | * @param string $locale The value to use as filter. |
||
| 337 | * Accepts wildcards (* and % trigger a LIKE) |
||
| 338 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
| 339 | * |
||
| 340 | * @return $this|ChildBannerImageI18nQuery The current query, for fluid interface |
||
| 341 | */ |
||
| 342 | View Code Duplication | public function filterByLocale($locale = null, $comparison = null) |
|
| 343 | { |
||
| 344 | if (null === $comparison) { |
||
| 345 | if (is_array($locale)) { |
||
| 346 | $comparison = Criteria::IN; |
||
| 347 | } elseif (preg_match('/[\%\*]/', $locale)) { |
||
| 348 | $locale = str_replace('*', '%', $locale); |
||
| 349 | $comparison = Criteria::LIKE; |
||
| 350 | } |
||
| 351 | } |
||
| 352 | |||
| 353 | return $this->addUsingAlias(BannerImageI18nTableMap::COL_LOCALE, $locale, $comparison); |
||
| 354 | } |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Filter the query on the src column |
||
| 358 | * |
||
| 359 | * Example usage: |
||
| 360 | * <code> |
||
| 361 | * $query->filterBySrc('fooValue'); // WHERE src = 'fooValue' |
||
| 362 | * $query->filterBySrc('%fooValue%'); // WHERE src LIKE '%fooValue%' |
||
| 363 | * </code> |
||
| 364 | * |
||
| 365 | * @param string $src The value to use as filter. |
||
| 366 | * Accepts wildcards (* and % trigger a LIKE) |
||
| 367 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
| 368 | * |
||
| 369 | * @return $this|ChildBannerImageI18nQuery The current query, for fluid interface |
||
| 370 | */ |
||
| 371 | View Code Duplication | public function filterBySrc($src = null, $comparison = null) |
|
| 372 | { |
||
| 373 | if (null === $comparison) { |
||
| 374 | if (is_array($src)) { |
||
| 375 | $comparison = Criteria::IN; |
||
| 376 | } elseif (preg_match('/[\%\*]/', $src)) { |
||
| 377 | $src = str_replace('*', '%', $src); |
||
| 378 | $comparison = Criteria::LIKE; |
||
| 379 | } |
||
| 380 | } |
||
| 381 | |||
| 382 | return $this->addUsingAlias(BannerImageI18nTableMap::COL_SRC, $src, $comparison); |
||
| 383 | } |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Filter the query on the name column |
||
| 387 | * |
||
| 388 | * Example usage: |
||
| 389 | * <code> |
||
| 390 | * $query->filterByName('fooValue'); // WHERE name = 'fooValue' |
||
| 391 | * $query->filterByName('%fooValue%'); // WHERE name LIKE '%fooValue%' |
||
| 392 | * </code> |
||
| 393 | * |
||
| 394 | * @param string $name The value to use as filter. |
||
| 395 | * Accepts wildcards (* and % trigger a LIKE) |
||
| 396 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
| 397 | * |
||
| 398 | * @return $this|ChildBannerImageI18nQuery The current query, for fluid interface |
||
| 399 | */ |
||
| 400 | View Code Duplication | public function filterByName($name = null, $comparison = null) |
|
| 401 | { |
||
| 402 | if (null === $comparison) { |
||
| 403 | if (is_array($name)) { |
||
| 404 | $comparison = Criteria::IN; |
||
| 405 | } elseif (preg_match('/[\%\*]/', $name)) { |
||
| 406 | $name = str_replace('*', '%', $name); |
||
| 407 | $comparison = Criteria::LIKE; |
||
| 408 | } |
||
| 409 | } |
||
| 410 | |||
| 411 | return $this->addUsingAlias(BannerImageI18nTableMap::COL_NAME, $name, $comparison); |
||
| 412 | } |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Filter the query on the clicks column |
||
| 416 | * |
||
| 417 | * Example usage: |
||
| 418 | * <code> |
||
| 419 | * $query->filterByClicks(1234); // WHERE clicks = 1234 |
||
| 420 | * $query->filterByClicks(array(12, 34)); // WHERE clicks IN (12, 34) |
||
| 421 | * $query->filterByClicks(array('min' => 12)); // WHERE clicks > 12 |
||
| 422 | * </code> |
||
| 423 | * |
||
| 424 | * @param mixed $clicks The value to use as filter. |
||
| 425 | * Use scalar values for equality. |
||
| 426 | * Use array values for in_array() equivalent. |
||
| 427 | * Use associative array('min' => $minValue, 'max' => $maxValue) for intervals. |
||
| 428 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
| 429 | * |
||
| 430 | * @return $this|ChildBannerImageI18nQuery The current query, for fluid interface |
||
| 431 | */ |
||
| 432 | View Code Duplication | public function filterByClicks($clicks = null, $comparison = null) |
|
| 454 | |||
| 455 | /** |
||
| 456 | * Filter the query on the description column |
||
| 457 | * |
||
| 458 | * Example usage: |
||
| 459 | * <code> |
||
| 460 | * $query->filterByDescription('fooValue'); // WHERE description = 'fooValue' |
||
| 461 | * $query->filterByDescription('%fooValue%'); // WHERE description LIKE '%fooValue%' |
||
| 462 | * </code> |
||
| 463 | * |
||
| 464 | * @param string $description The value to use as filter. |
||
| 465 | * Accepts wildcards (* and % trigger a LIKE) |
||
| 466 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
| 467 | * |
||
| 468 | * @return $this|ChildBannerImageI18nQuery The current query, for fluid interface |
||
| 469 | */ |
||
| 470 | View Code Duplication | public function filterByDescription($description = null, $comparison = null) |
|
| 471 | { |
||
| 472 | if (null === $comparison) { |
||
| 473 | if (is_array($description)) { |
||
| 474 | $comparison = Criteria::IN; |
||
| 475 | } elseif (preg_match('/[\%\*]/', $description)) { |
||
| 476 | $description = str_replace('*', '%', $description); |
||
| 477 | $comparison = Criteria::LIKE; |
||
| 478 | } |
||
| 479 | } |
||
| 480 | |||
| 481 | return $this->addUsingAlias(BannerImageI18nTableMap::COL_DESCRIPTION, $description, $comparison); |
||
| 482 | } |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Filter the query by a related \xbanners\models\BannerImage object |
||
| 486 | * |
||
| 487 | * @param \xbanners\models\BannerImage|ObjectCollection $bannerImage The related object(s) to use as filter |
||
| 488 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
| 489 | * |
||
| 490 | * @throws \Propel\Runtime\Exception\PropelException |
||
| 491 | * |
||
| 492 | * @return ChildBannerImageI18nQuery The current query, for fluid interface |
||
| 493 | */ |
||
| 494 | public function filterByBannerImage($bannerImage, $comparison = null) |
||
| 495 | { |
||
| 496 | if ($bannerImage instanceof \xbanners\models\BannerImage) { |
||
| 497 | return $this |
||
| 498 | ->addUsingAlias(BannerImageI18nTableMap::COL_ID, $bannerImage->getId(), $comparison); |
||
| 499 | } elseif ($bannerImage instanceof ObjectCollection) { |
||
| 500 | if (null === $comparison) { |
||
| 501 | $comparison = Criteria::IN; |
||
| 502 | } |
||
| 503 | |||
| 504 | return $this |
||
| 505 | ->addUsingAlias(BannerImageI18nTableMap::COL_ID, $bannerImage->toKeyValue('PrimaryKey', 'Id'), $comparison); |
||
| 506 | } else { |
||
| 507 | throw new PropelException('filterByBannerImage() only accepts arguments of type \xbanners\models\BannerImage or Collection'); |
||
| 508 | } |
||
| 509 | } |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Adds a JOIN clause to the query using the BannerImage relation |
||
| 513 | * |
||
| 514 | * @param string $relationAlias optional alias for the relation |
||
| 515 | * @param string $joinType Accepted values are null, 'left join', 'right join', 'inner join' |
||
| 516 | * |
||
| 517 | * @return $this|ChildBannerImageI18nQuery The current query, for fluid interface |
||
| 518 | */ |
||
| 519 | public function joinBannerImage($relationAlias = null, $joinType = 'LEFT JOIN') |
||
| 520 | { |
||
| 521 | $tableMap = $this->getTableMap(); |
||
| 522 | $relationMap = $tableMap->getRelation('BannerImage'); |
||
| 523 | |||
| 524 | // create a ModelJoin object for this join |
||
| 525 | $join = new ModelJoin(); |
||
| 526 | $join->setJoinType($joinType); |
||
| 527 | $join->setRelationMap($relationMap, $this->useAliasInSQL ? $this->getModelAlias() : null, $relationAlias); |
||
| 528 | if ($previousJoin = $this->getPreviousJoin()) { |
||
| 529 | $join->setPreviousJoin($previousJoin); |
||
| 530 | } |
||
| 531 | |||
| 532 | // add the ModelJoin to the current object |
||
| 533 | if ($relationAlias) { |
||
| 534 | $this->addAlias($relationAlias, $relationMap->getRightTable()->getName()); |
||
| 535 | $this->addJoinObject($join, $relationAlias); |
||
| 536 | } else { |
||
| 537 | $this->addJoinObject($join, 'BannerImage'); |
||
| 538 | } |
||
| 539 | |||
| 540 | return $this; |
||
| 541 | } |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Use the BannerImage relation BannerImage object |
||
| 545 | * |
||
| 546 | * @see useQuery() |
||
| 547 | * |
||
| 548 | * @param string $relationAlias optional alias for the relation, |
||
| 549 | * to be used as main alias in the secondary query |
||
| 550 | * @param string $joinType Accepted values are null, 'left join', 'right join', 'inner join' |
||
| 551 | * |
||
| 552 | * @return \xbanners\models\BannerImageQuery A secondary query class using the current class as primary query |
||
| 553 | */ |
||
| 554 | public function useBannerImageQuery($relationAlias = null, $joinType = 'LEFT JOIN') |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Exclude object from result |
||
| 563 | * |
||
| 564 | * @param ChildBannerImageI18n $bannerImageI18n Object to remove from the list of results |
||
| 565 | * |
||
| 566 | * @return $this|ChildBannerImageI18nQuery The current query, for fluid interface |
||
| 567 | */ |
||
| 568 | public function prune($bannerImageI18n = null) |
||
| 569 | { |
||
| 570 | if ($bannerImageI18n) { |
||
| 571 | $this->addCond('pruneCond0', $this->getAliasedColName(BannerImageI18nTableMap::COL_ID), $bannerImageI18n->getId(), Criteria::NOT_EQUAL); |
||
| 572 | $this->addCond('pruneCond1', $this->getAliasedColName(BannerImageI18nTableMap::COL_LOCALE), $bannerImageI18n->getLocale(), Criteria::NOT_EQUAL); |
||
| 573 | $this->combine(array('pruneCond0', 'pruneCond1'), Criteria::LOGICAL_OR); |
||
| 574 | } |
||
| 575 | |||
| 576 | return $this; |
||
| 577 | } |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Deletes all rows from the banner_image_i18n table. |
||
| 581 | * |
||
| 582 | * @param ConnectionInterface $con the connection to use |
||
| 583 | * @return int The number of affected rows (if supported by underlying database driver). |
||
| 584 | */ |
||
| 585 | public function doDeleteAll(ConnectionInterface $con = null) |
||
| 605 | |||
| 606 | /** |
||
| 607 | * Performs a DELETE on the database based on the current ModelCriteria |
||
| 608 | * |
||
| 609 | * @param ConnectionInterface $con the connection to use |
||
| 610 | * @return int The number of affected rows (if supported by underlying database driver). This includes CASCADE-related rows |
||
| 611 | * if supported by native driver or if emulated using Propel. |
||
| 612 | * @throws PropelException Any exceptions caught during processing will be |
||
| 613 | * rethrown wrapped into a PropelException. |
||
| 614 | */ |
||
| 615 | public function delete(ConnectionInterface $con = null) |
||
| 616 | { |
||
| 639 | |||
| 640 | } // BannerImageI18nQuery |
||
| 641 |