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 BannersI18nQuery 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 BannersI18nQuery, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 72 | abstract class BannersI18nQuery extends ModelCriteria |
||
| 73 | { |
||
| 74 | protected $entityNotFoundExceptionClass = '\\Propel\\Runtime\\Exception\\EntityNotFoundException'; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Initializes internal state of \xbanners\models\Base\BannersI18nQuery object. |
||
| 78 | * |
||
| 79 | * @param string $dbName The database name |
||
| 80 | * @param string $modelName The phpName of a model, e.g. 'Book' |
||
| 81 | * @param string $modelAlias The alias for the model in this query, e.g. 'b' |
||
| 82 | */ |
||
| 83 | public function __construct($dbName = 'Shop', $modelName = '\\xbanners\\models\\BannersI18n', $modelAlias = null) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Returns a new ChildBannersI18nQuery object. |
||
| 90 | * |
||
| 91 | * @param string $modelAlias The alias of a model in the query |
||
| 92 | * @param Criteria $criteria Optional Criteria to build the query from |
||
| 93 | * |
||
| 94 | * @return ChildBannersI18nQuery |
||
| 95 | */ |
||
| 96 | View Code Duplication | public static function create($modelAlias = null, Criteria $criteria = null) |
|
| 111 | |||
| 112 | /** |
||
| 113 | * Find object by primary key. |
||
| 114 | * Propel uses the instance pool to skip the database if the object exists. |
||
| 115 | * Go fast if the query is untouched. |
||
| 116 | * |
||
| 117 | * <code> |
||
| 118 | * $obj = $c->findPk(array(12, 34), $con); |
||
| 119 | * </code> |
||
| 120 | * |
||
| 121 | * @param array[$id, $locale] $key Primary key to use for the query |
||
| 122 | * @param ConnectionInterface $con an optional connection object |
||
| 123 | * |
||
| 124 | * @return ChildBannersI18n|array|mixed the result, formatted by the current formatter |
||
| 125 | */ |
||
| 126 | public function findPk($key, ConnectionInterface $con = null) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Find object by primary key using raw SQL to go fast. |
||
| 156 | * Bypass doSelect() and the object formatter by using generated code. |
||
| 157 | * |
||
| 158 | * @param mixed $key Primary key to use for the query |
||
| 159 | * @param ConnectionInterface $con A connection object |
||
| 160 | * |
||
| 161 | * @throws \Propel\Runtime\Exception\PropelException |
||
| 162 | * |
||
| 163 | * @return ChildBannersI18n A model object, or null if the key is not found |
||
| 164 | */ |
||
| 165 | protected function findPkSimple($key, ConnectionInterface $con) |
||
| 166 | { |
||
| 167 | $sql = 'SELECT id, locale, name FROM banners_i18n WHERE id = :p0 AND locale = :p1'; |
||
| 168 | try { |
||
| 169 | $stmt = $con->prepare($sql); |
||
| 170 | $stmt->bindValue(':p0', $key[0], PDO::PARAM_INT); |
||
| 171 | $stmt->bindValue(':p1', $key[1], PDO::PARAM_STR); |
||
| 172 | $stmt->execute(); |
||
| 173 | } catch (Exception $e) { |
||
| 174 | Propel::log($e->getMessage(), Propel::LOG_ERR); |
||
| 175 | throw new PropelException(sprintf('Unable to execute SELECT statement [%s]', $sql), 0, $e); |
||
| 176 | } |
||
| 177 | $obj = null; |
||
| 178 | if ($row = $stmt->fetch(\PDO::FETCH_NUM)) { |
||
| 179 | /** @var ChildBannersI18n $obj */ |
||
| 180 | $obj = new ChildBannersI18n(); |
||
| 181 | $obj->hydrate($row); |
||
| 182 | BannersI18nTableMap::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])])); |
||
| 183 | } |
||
| 184 | $stmt->closeCursor(); |
||
| 185 | |||
| 186 | return $obj; |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Find object by primary key. |
||
| 191 | * |
||
| 192 | * @param mixed $key Primary key to use for the query |
||
| 193 | * @param ConnectionInterface $con A connection object |
||
| 194 | * |
||
| 195 | * @return ChildBannersI18n|array|mixed the result, formatted by the current formatter |
||
| 196 | */ |
||
| 197 | protected function findPkComplex($key, ConnectionInterface $con) |
||
| 198 | { |
||
| 199 | // As the query uses a PK condition, no limit(1) is necessary. |
||
| 200 | $criteria = $this->isKeepQuery() ? clone $this : $this; |
||
| 201 | $dataFetcher = $criteria |
||
| 202 | ->filterByPrimaryKey($key) |
||
| 203 | ->doSelect($con); |
||
| 204 | |||
| 205 | return $criteria->getFormatter()->init($criteria)->formatOne($dataFetcher); |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Find objects by primary key |
||
| 210 | * <code> |
||
| 211 | * $objs = $c->findPks(array(array(12, 56), array(832, 123), array(123, 456)), $con); |
||
| 212 | * </code> |
||
| 213 | * @param array $keys Primary keys to use for the query |
||
| 214 | * @param ConnectionInterface $con an optional connection object |
||
| 215 | * |
||
| 216 | * @return ObjectCollection|array|mixed the list of results, formatted by the current formatter |
||
| 217 | */ |
||
| 218 | public function findPks($keys, ConnectionInterface $con = null) |
||
| 219 | { |
||
| 220 | if (null === $con) { |
||
| 221 | $con = Propel::getServiceContainer()->getReadConnection($this->getDbName()); |
||
| 222 | } |
||
| 223 | $this->basePreSelect($con); |
||
| 224 | $criteria = $this->isKeepQuery() ? clone $this : $this; |
||
| 225 | $dataFetcher = $criteria |
||
| 226 | ->filterByPrimaryKeys($keys) |
||
| 227 | ->doSelect($con); |
||
| 228 | |||
| 229 | return $criteria->getFormatter()->init($criteria)->format($dataFetcher); |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Filter the query by primary key |
||
| 234 | * |
||
| 235 | * @param mixed $key Primary key to use for the query |
||
| 236 | * |
||
| 237 | * @return $this|ChildBannersI18nQuery The current query, for fluid interface |
||
| 238 | */ |
||
| 239 | public function filterByPrimaryKey($key) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Filter the query by a list of primary keys |
||
| 249 | * |
||
| 250 | * @param array $keys The list of primary key to use for the query |
||
| 251 | * |
||
| 252 | * @return $this|ChildBannersI18nQuery The current query, for fluid interface |
||
| 253 | */ |
||
| 254 | public function filterByPrimaryKeys($keys) |
||
| 255 | { |
||
| 256 | if (empty($keys)) { |
||
| 257 | return $this->add(null, '1<>1', Criteria::CUSTOM); |
||
| 258 | } |
||
| 259 | foreach ($keys as $key) { |
||
| 260 | $cton0 = $this->getNewCriterion(BannersI18nTableMap::COL_ID, $key[0], Criteria::EQUAL); |
||
| 261 | $cton1 = $this->getNewCriterion(BannersI18nTableMap::COL_LOCALE, $key[1], Criteria::EQUAL); |
||
| 262 | $cton0->addAnd($cton1); |
||
| 263 | $this->addOr($cton0); |
||
| 264 | } |
||
| 265 | |||
| 266 | return $this; |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Filter the query on the id column |
||
| 271 | * |
||
| 272 | * Example usage: |
||
| 273 | * <code> |
||
| 274 | * $query->filterById(1234); // WHERE id = 1234 |
||
| 275 | * $query->filterById(array(12, 34)); // WHERE id IN (12, 34) |
||
| 276 | * $query->filterById(array('min' => 12)); // WHERE id > 12 |
||
| 277 | * </code> |
||
| 278 | * |
||
| 279 | * @see filterByBanners() |
||
| 280 | * |
||
| 281 | * @param mixed $id The value to use as filter. |
||
| 282 | * Use scalar values for equality. |
||
| 283 | * Use array values for in_array() equivalent. |
||
| 284 | * Use associative array('min' => $minValue, 'max' => $maxValue) for intervals. |
||
| 285 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
| 286 | * |
||
| 287 | * @return $this|ChildBannersI18nQuery The current query, for fluid interface |
||
| 288 | */ |
||
| 289 | View Code Duplication | public function filterById($id = null, $comparison = null) |
|
| 311 | |||
| 312 | /** |
||
| 313 | * Filter the query on the locale column |
||
| 314 | * |
||
| 315 | * Example usage: |
||
| 316 | * <code> |
||
| 317 | * $query->filterByLocale('fooValue'); // WHERE locale = 'fooValue' |
||
| 318 | * $query->filterByLocale('%fooValue%'); // WHERE locale LIKE '%fooValue%' |
||
| 319 | * </code> |
||
| 320 | * |
||
| 321 | * @param string $locale The value to use as filter. |
||
| 322 | * Accepts wildcards (* and % trigger a LIKE) |
||
| 323 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
| 324 | * |
||
| 325 | * @return $this|ChildBannersI18nQuery The current query, for fluid interface |
||
| 326 | */ |
||
| 327 | View Code Duplication | public function filterByLocale($locale = null, $comparison = null) |
|
| 328 | { |
||
| 329 | if (null === $comparison) { |
||
| 330 | if (is_array($locale)) { |
||
| 331 | $comparison = Criteria::IN; |
||
| 332 | } elseif (preg_match('/[\%\*]/', $locale)) { |
||
| 333 | $locale = str_replace('*', '%', $locale); |
||
| 334 | $comparison = Criteria::LIKE; |
||
| 335 | } |
||
| 336 | } |
||
| 337 | |||
| 338 | return $this->addUsingAlias(BannersI18nTableMap::COL_LOCALE, $locale, $comparison); |
||
| 339 | } |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Filter the query on the name column |
||
| 343 | * |
||
| 344 | * Example usage: |
||
| 345 | * <code> |
||
| 346 | * $query->filterByName('fooValue'); // WHERE name = 'fooValue' |
||
| 347 | * $query->filterByName('%fooValue%'); // WHERE name LIKE '%fooValue%' |
||
| 348 | * </code> |
||
| 349 | * |
||
| 350 | * @param string $name The value to use as filter. |
||
| 351 | * Accepts wildcards (* and % trigger a LIKE) |
||
| 352 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
| 353 | * |
||
| 354 | * @return $this|ChildBannersI18nQuery The current query, for fluid interface |
||
| 355 | */ |
||
| 356 | View Code Duplication | public function filterByName($name = null, $comparison = null) |
|
| 357 | { |
||
| 358 | if (null === $comparison) { |
||
| 359 | if (is_array($name)) { |
||
| 360 | $comparison = Criteria::IN; |
||
| 361 | } elseif (preg_match('/[\%\*]/', $name)) { |
||
| 362 | $name = str_replace('*', '%', $name); |
||
| 363 | $comparison = Criteria::LIKE; |
||
| 364 | } |
||
| 365 | } |
||
| 366 | |||
| 367 | return $this->addUsingAlias(BannersI18nTableMap::COL_NAME, $name, $comparison); |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Filter the query by a related \xbanners\models\Banners object |
||
| 372 | * |
||
| 373 | * @param \xbanners\models\Banners|ObjectCollection $banners The related object(s) to use as filter |
||
| 374 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
| 375 | * |
||
| 376 | * @throws \Propel\Runtime\Exception\PropelException |
||
| 377 | * |
||
| 378 | * @return ChildBannersI18nQuery The current query, for fluid interface |
||
| 379 | */ |
||
| 380 | public function filterByBanners($banners, $comparison = null) |
||
| 381 | { |
||
| 382 | if ($banners instanceof \xbanners\models\Banners) { |
||
| 383 | return $this |
||
| 384 | ->addUsingAlias(BannersI18nTableMap::COL_ID, $banners->getId(), $comparison); |
||
| 385 | } elseif ($banners instanceof ObjectCollection) { |
||
| 386 | if (null === $comparison) { |
||
| 387 | $comparison = Criteria::IN; |
||
| 388 | } |
||
| 389 | |||
| 390 | return $this |
||
| 391 | ->addUsingAlias(BannersI18nTableMap::COL_ID, $banners->toKeyValue('PrimaryKey', 'Id'), $comparison); |
||
| 392 | } else { |
||
| 393 | throw new PropelException('filterByBanners() only accepts arguments of type \xbanners\models\Banners or Collection'); |
||
| 394 | } |
||
| 395 | } |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Adds a JOIN clause to the query using the Banners relation |
||
| 399 | * |
||
| 400 | * @param string $relationAlias optional alias for the relation |
||
| 401 | * @param string $joinType Accepted values are null, 'left join', 'right join', 'inner join' |
||
| 402 | * |
||
| 403 | * @return $this|ChildBannersI18nQuery The current query, for fluid interface |
||
| 404 | */ |
||
| 405 | public function joinBanners($relationAlias = null, $joinType = 'LEFT JOIN') |
||
| 406 | { |
||
| 407 | $tableMap = $this->getTableMap(); |
||
| 408 | $relationMap = $tableMap->getRelation('Banners'); |
||
| 409 | |||
| 410 | // create a ModelJoin object for this join |
||
| 411 | $join = new ModelJoin(); |
||
| 412 | $join->setJoinType($joinType); |
||
| 413 | $join->setRelationMap($relationMap, $this->useAliasInSQL ? $this->getModelAlias() : null, $relationAlias); |
||
| 414 | if ($previousJoin = $this->getPreviousJoin()) { |
||
| 415 | $join->setPreviousJoin($previousJoin); |
||
| 416 | } |
||
| 417 | |||
| 418 | // add the ModelJoin to the current object |
||
| 419 | if ($relationAlias) { |
||
| 420 | $this->addAlias($relationAlias, $relationMap->getRightTable()->getName()); |
||
| 421 | $this->addJoinObject($join, $relationAlias); |
||
| 422 | } else { |
||
| 423 | $this->addJoinObject($join, 'Banners'); |
||
| 424 | } |
||
| 425 | |||
| 426 | return $this; |
||
| 427 | } |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Use the Banners relation Banners object |
||
| 431 | * |
||
| 432 | * @see useQuery() |
||
| 433 | * |
||
| 434 | * @param string $relationAlias optional alias for the relation, |
||
| 435 | * to be used as main alias in the secondary query |
||
| 436 | * @param string $joinType Accepted values are null, 'left join', 'right join', 'inner join' |
||
| 437 | * |
||
| 438 | * @return \xbanners\models\BannersQuery A secondary query class using the current class as primary query |
||
| 439 | */ |
||
| 440 | public function useBannersQuery($relationAlias = null, $joinType = 'LEFT JOIN') |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Exclude object from result |
||
| 449 | * |
||
| 450 | * @param ChildBannersI18n $bannersI18n Object to remove from the list of results |
||
| 451 | * |
||
| 452 | * @return $this|ChildBannersI18nQuery The current query, for fluid interface |
||
| 453 | */ |
||
| 454 | public function prune($bannersI18n = null) |
||
| 455 | { |
||
| 456 | if ($bannersI18n) { |
||
| 457 | $this->addCond('pruneCond0', $this->getAliasedColName(BannersI18nTableMap::COL_ID), $bannersI18n->getId(), Criteria::NOT_EQUAL); |
||
| 458 | $this->addCond('pruneCond1', $this->getAliasedColName(BannersI18nTableMap::COL_LOCALE), $bannersI18n->getLocale(), Criteria::NOT_EQUAL); |
||
| 459 | $this->combine(array('pruneCond0', 'pruneCond1'), Criteria::LOGICAL_OR); |
||
| 460 | } |
||
| 461 | |||
| 462 | return $this; |
||
| 463 | } |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Deletes all rows from the banners_i18n table. |
||
| 467 | * |
||
| 468 | * @param ConnectionInterface $con the connection to use |
||
| 469 | * @return int The number of affected rows (if supported by underlying database driver). |
||
| 470 | */ |
||
| 471 | public function doDeleteAll(ConnectionInterface $con = null) |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Performs a DELETE on the database based on the current ModelCriteria |
||
| 494 | * |
||
| 495 | * @param ConnectionInterface $con the connection to use |
||
| 496 | * @return int The number of affected rows (if supported by underlying database driver). This includes CASCADE-related rows |
||
| 497 | * if supported by native driver or if emulated using Propel. |
||
| 498 | * @throws PropelException Any exceptions caught during processing will be |
||
| 499 | * rethrown wrapped into a PropelException. |
||
| 500 | */ |
||
| 501 | public function delete(ConnectionInterface $con = null) |
||
| 525 | |||
| 526 | } // BannersI18nQuery |
||
| 527 |