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 BannerImageI18nTableMap 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 BannerImageI18nTableMap, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class BannerImageI18nTableMap extends TableMap |
||
30 | { |
||
31 | use InstancePoolTrait; |
||
32 | use TableMapTrait; |
||
33 | |||
34 | /** |
||
35 | * The (dot-path) name of this class |
||
36 | */ |
||
37 | const CLASS_NAME = 'xbanners.models.Map.BannerImageI18nTableMap'; |
||
38 | |||
39 | /** |
||
40 | * The default database name for this class |
||
41 | */ |
||
42 | const DATABASE_NAME = 'Shop'; |
||
43 | |||
44 | /** |
||
45 | * The table name for this class |
||
46 | */ |
||
47 | const TABLE_NAME = 'banner_image_i18n'; |
||
48 | |||
49 | /** |
||
50 | * The related Propel class for this table |
||
51 | */ |
||
52 | const OM_CLASS = '\\xbanners\\models\\BannerImageI18n'; |
||
53 | |||
54 | /** |
||
55 | * A class that can be returned by this tableMap |
||
56 | */ |
||
57 | const CLASS_DEFAULT = 'xbanners.models.BannerImageI18n'; |
||
58 | |||
59 | /** |
||
60 | * The total number of columns |
||
61 | */ |
||
62 | const NUM_COLUMNS = 6; |
||
63 | |||
64 | /** |
||
65 | * The number of lazy-loaded columns |
||
66 | */ |
||
67 | const NUM_LAZY_LOAD_COLUMNS = 0; |
||
68 | |||
69 | /** |
||
70 | * The number of columns to hydrate (NUM_COLUMNS - NUM_LAZY_LOAD_COLUMNS) |
||
71 | */ |
||
72 | const NUM_HYDRATE_COLUMNS = 6; |
||
73 | |||
74 | /** |
||
75 | * the column name for the id field |
||
76 | */ |
||
77 | const COL_ID = 'banner_image_i18n.id'; |
||
78 | |||
79 | /** |
||
80 | * the column name for the locale field |
||
81 | */ |
||
82 | const COL_LOCALE = 'banner_image_i18n.locale'; |
||
83 | |||
84 | /** |
||
85 | * the column name for the src field |
||
86 | */ |
||
87 | const COL_SRC = 'banner_image_i18n.src'; |
||
88 | |||
89 | /** |
||
90 | * the column name for the name field |
||
91 | */ |
||
92 | const COL_NAME = 'banner_image_i18n.name'; |
||
93 | |||
94 | /** |
||
95 | * the column name for the clicks field |
||
96 | */ |
||
97 | const COL_CLICKS = 'banner_image_i18n.clicks'; |
||
98 | |||
99 | /** |
||
100 | * the column name for the description field |
||
101 | */ |
||
102 | const COL_DESCRIPTION = 'banner_image_i18n.description'; |
||
103 | |||
104 | /** |
||
105 | * The default string format for model objects of the related table |
||
106 | */ |
||
107 | const DEFAULT_STRING_FORMAT = 'YAML'; |
||
108 | |||
109 | /** |
||
110 | * holds an array of fieldnames |
||
111 | * |
||
112 | * first dimension keys are the type constants |
||
113 | * e.g. self::$fieldNames[self::TYPE_PHPNAME][0] = 'Id' |
||
114 | */ |
||
115 | protected static $fieldNames = array ( |
||
116 | self::TYPE_PHPNAME => array('Id', 'Locale', 'Src', 'Name', 'Clicks', 'Description', ), |
||
117 | self::TYPE_CAMELNAME => array('id', 'locale', 'src', 'name', 'clicks', 'description', ), |
||
118 | self::TYPE_COLNAME => array(BannerImageI18nTableMap::COL_ID, BannerImageI18nTableMap::COL_LOCALE, BannerImageI18nTableMap::COL_SRC, BannerImageI18nTableMap::COL_NAME, BannerImageI18nTableMap::COL_CLICKS, BannerImageI18nTableMap::COL_DESCRIPTION, ), |
||
119 | self::TYPE_FIELDNAME => array('id', 'locale', 'src', 'name', 'clicks', 'description', ), |
||
120 | self::TYPE_NUM => array(0, 1, 2, 3, 4, 5, ) |
||
121 | ); |
||
122 | |||
123 | /** |
||
124 | * holds an array of keys for quick access to the fieldnames array |
||
125 | * |
||
126 | * first dimension keys are the type constants |
||
127 | * e.g. self::$fieldKeys[self::TYPE_PHPNAME]['Id'] = 0 |
||
128 | */ |
||
129 | protected static $fieldKeys = array ( |
||
130 | self::TYPE_PHPNAME => array('Id' => 0, 'Locale' => 1, 'Src' => 2, 'Name' => 3, 'Clicks' => 4, 'Description' => 5, ), |
||
131 | self::TYPE_CAMELNAME => array('id' => 0, 'locale' => 1, 'src' => 2, 'name' => 3, 'clicks' => 4, 'description' => 5, ), |
||
132 | self::TYPE_COLNAME => array(BannerImageI18nTableMap::COL_ID => 0, BannerImageI18nTableMap::COL_LOCALE => 1, BannerImageI18nTableMap::COL_SRC => 2, BannerImageI18nTableMap::COL_NAME => 3, BannerImageI18nTableMap::COL_CLICKS => 4, BannerImageI18nTableMap::COL_DESCRIPTION => 5, ), |
||
133 | self::TYPE_FIELDNAME => array('id' => 0, 'locale' => 1, 'src' => 2, 'name' => 3, 'clicks' => 4, 'description' => 5, ), |
||
134 | self::TYPE_NUM => array(0, 1, 2, 3, 4, 5, ) |
||
135 | ); |
||
136 | |||
137 | /** |
||
138 | * Initialize the table attributes and columns |
||
139 | * Relations are not initialized by this method since they are lazy loaded |
||
140 | * |
||
141 | * @return void |
||
142 | * @throws PropelException |
||
143 | */ |
||
144 | View Code Duplication | public function initialize() |
|
145 | { |
||
146 | // attributes |
||
147 | $this->setName('banner_image_i18n'); |
||
148 | $this->setPhpName('BannerImageI18n'); |
||
149 | $this->setIdentifierQuoting(false); |
||
150 | $this->setClassName('\\xbanners\\models\\BannerImageI18n'); |
||
151 | $this->setPackage('xbanners.models'); |
||
152 | $this->setUseIdGenerator(false); |
||
153 | // columns |
||
154 | $this->addForeignPrimaryKey('id', 'Id', 'INTEGER' , 'banner_image', 'id', true, 11, null); |
||
155 | $this->addPrimaryKey('locale', 'Locale', 'VARCHAR', true, 5, 'ru'); |
||
156 | $this->addColumn('src', 'Src', 'VARCHAR', false, 255, null); |
||
157 | $this->addColumn('name', 'Name', 'VARCHAR', false, 255, null); |
||
158 | $this->addColumn('clicks', 'Clicks', 'INTEGER', false, 20, null); |
||
159 | $this->addColumn('description', 'Description', 'LONGVARCHAR', false, null, null); |
||
160 | } // initialize() |
||
161 | |||
162 | /** |
||
163 | * Build the RelationMap objects for this table relationships |
||
164 | */ |
||
165 | View Code Duplication | public function buildRelations() |
|
175 | |||
176 | /** |
||
177 | * Adds an object to the instance pool. |
||
178 | * |
||
179 | * Propel keeps cached copies of objects in an instance pool when they are retrieved |
||
180 | * from the database. In some cases you may need to explicitly add objects |
||
181 | * to the cache in order to ensure that the same objects are always returned by find*() |
||
182 | * and findPk*() calls. |
||
183 | * |
||
184 | * @param \xbanners\models\BannerImageI18n $obj A \xbanners\models\BannerImageI18n object. |
||
185 | * @param string $key (optional) key to use for instance map (for performance boost if key was already calculated externally). |
||
186 | */ |
||
187 | View Code Duplication | public static function addInstanceToPool($obj, $key = null) |
|
196 | |||
197 | /** |
||
198 | * Removes an object from the instance pool. |
||
199 | * |
||
200 | * Propel keeps cached copies of objects in an instance pool when they are retrieved |
||
201 | * from the database. In some cases -- especially when you override doDelete |
||
202 | * methods in your stub classes -- you may need to explicitly remove objects |
||
203 | * from the cache in order to prevent returning objects that no longer exist. |
||
204 | * |
||
205 | * @param mixed $value A \xbanners\models\BannerImageI18n object or a primary key value. |
||
206 | */ |
||
207 | public static function removeInstanceFromPool($value) |
||
208 | { |
||
209 | if (Propel::isInstancePoolingEnabled() && null !== $value) { |
||
210 | if (is_object($value) && $value instanceof \xbanners\models\BannerImageI18n) { |
||
211 | $key = serialize([(null === $value->getId() || is_scalar($value->getId()) || is_callable([$value->getId(), '__toString']) ? (string) $value->getId() : $value->getId()), (null === $value->getLocale() || is_scalar($value->getLocale()) || is_callable([$value->getLocale(), '__toString']) ? (string) $value->getLocale() : $value->getLocale())]); |
||
212 | |||
213 | } elseif (is_array($value) && count($value) === 2) { |
||
214 | // assume we've been passed a primary key"; |
||
215 | $key = serialize([(null === $value[0] || is_scalar($value[0]) || is_callable([$value[0], '__toString']) ? (string) $value[0] : $value[0]), (null === $value[1] || is_scalar($value[1]) || is_callable([$value[1], '__toString']) ? (string) $value[1] : $value[1])]); |
||
216 | } elseif ($value instanceof Criteria) { |
||
217 | self::$instances = []; |
||
218 | |||
219 | return; |
||
220 | } else { |
||
221 | $e = new PropelException("Invalid value passed to removeInstanceFromPool(). Expected primary key or \xbanners\models\BannerImageI18n object; got " . (is_object($value) ? get_class($value) . ' object.' : var_export($value, true))); |
||
222 | throw $e; |
||
223 | } |
||
224 | |||
225 | unset(self::$instances[$key]); |
||
226 | } |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * Retrieves a string version of the primary key from the DB resultset row that can be used to uniquely identify a row in this table. |
||
231 | * |
||
232 | * For tables with a single-column primary key, that simple pkey value will be returned. For tables with |
||
233 | * a multi-column primary key, a serialize()d version of the primary key will be returned. |
||
234 | * |
||
235 | * @param array $row resultset row. |
||
236 | * @param int $offset The 0-based offset for reading from the resultset row. |
||
237 | * @param string $indexType One of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_CAMELNAME |
||
238 | * TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM |
||
239 | * |
||
240 | * @return string The primary key hash of the row |
||
241 | */ |
||
242 | public static function getPrimaryKeyHashFromRow($row, $offset = 0, $indexType = TableMap::TYPE_NUM) |
||
243 | { |
||
244 | // If the PK cannot be derived from the row, return NULL. |
||
245 | if ($row[TableMap::TYPE_NUM == $indexType ? 0 + $offset : static::translateFieldName('Id', TableMap::TYPE_PHPNAME, $indexType)] === null && $row[TableMap::TYPE_NUM == $indexType ? 1 + $offset : static::translateFieldName('Locale', TableMap::TYPE_PHPNAME, $indexType)] === null) { |
||
246 | return null; |
||
247 | } |
||
248 | |||
249 | return serialize([(null === $row[TableMap::TYPE_NUM == $indexType ? 0 + $offset : static::translateFieldName('Id', TableMap::TYPE_PHPNAME, $indexType)] || is_scalar($row[TableMap::TYPE_NUM == $indexType ? 0 + $offset : static::translateFieldName('Id', TableMap::TYPE_PHPNAME, $indexType)]) || is_callable([$row[TableMap::TYPE_NUM == $indexType ? 0 + $offset : static::translateFieldName('Id', TableMap::TYPE_PHPNAME, $indexType)], '__toString']) ? (string) $row[TableMap::TYPE_NUM == $indexType ? 0 + $offset : static::translateFieldName('Id', TableMap::TYPE_PHPNAME, $indexType)] : $row[TableMap::TYPE_NUM == $indexType ? 0 + $offset : static::translateFieldName('Id', TableMap::TYPE_PHPNAME, $indexType)]), (null === $row[TableMap::TYPE_NUM == $indexType ? 1 + $offset : static::translateFieldName('Locale', TableMap::TYPE_PHPNAME, $indexType)] || is_scalar($row[TableMap::TYPE_NUM == $indexType ? 1 + $offset : static::translateFieldName('Locale', TableMap::TYPE_PHPNAME, $indexType)]) || is_callable([$row[TableMap::TYPE_NUM == $indexType ? 1 + $offset : static::translateFieldName('Locale', TableMap::TYPE_PHPNAME, $indexType)], '__toString']) ? (string) $row[TableMap::TYPE_NUM == $indexType ? 1 + $offset : static::translateFieldName('Locale', TableMap::TYPE_PHPNAME, $indexType)] : $row[TableMap::TYPE_NUM == $indexType ? 1 + $offset : static::translateFieldName('Locale', TableMap::TYPE_PHPNAME, $indexType)])]); |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * Retrieves the primary key from the DB resultset row |
||
254 | * For tables with a single-column primary key, that simple pkey value will be returned. For tables with |
||
255 | * a multi-column primary key, an array of the primary key columns will be returned. |
||
256 | * |
||
257 | * @param array $row resultset row. |
||
258 | * @param int $offset The 0-based offset for reading from the resultset row. |
||
259 | * @param string $indexType One of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_CAMELNAME |
||
260 | * TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM |
||
261 | * |
||
262 | * @return mixed The primary key of the row |
||
263 | */ |
||
264 | public static function getPrimaryKeyFromRow($row, $offset = 0, $indexType = TableMap::TYPE_NUM) |
||
265 | { |
||
266 | $pks = []; |
||
267 | |||
268 | $pks[] = (int) $row[ |
||
269 | $indexType == TableMap::TYPE_NUM |
||
270 | ? 0 + $offset |
||
271 | : self::translateFieldName('Id', TableMap::TYPE_PHPNAME, $indexType) |
||
272 | ]; |
||
273 | $pks[] = (string) $row[ |
||
274 | $indexType == TableMap::TYPE_NUM |
||
275 | ? 1 + $offset |
||
276 | : self::translateFieldName('Locale', TableMap::TYPE_PHPNAME, $indexType) |
||
277 | ]; |
||
278 | |||
279 | return $pks; |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * The class that the tableMap will make instances of. |
||
284 | * |
||
285 | * If $withPrefix is true, the returned path |
||
286 | * uses a dot-path notation which is translated into a path |
||
287 | * relative to a location on the PHP include_path. |
||
288 | * (e.g. path.to.MyClass -> 'path/to/MyClass.php') |
||
289 | * |
||
290 | * @param boolean $withPrefix Whether or not to return the path with the class name |
||
291 | * @return string path.to.ClassName |
||
292 | */ |
||
293 | public static function getOMClass($withPrefix = true) |
||
297 | |||
298 | /** |
||
299 | * Populates an object of the default type or an object that inherit from the default. |
||
300 | * |
||
301 | * @param array $row row returned by DataFetcher->fetch(). |
||
302 | * @param int $offset The 0-based offset for reading from the resultset row. |
||
303 | * @param string $indexType The index type of $row. Mostly DataFetcher->getIndexType(). |
||
304 | One of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_CAMELNAME |
||
305 | * TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM. |
||
306 | * |
||
307 | * @throws PropelException Any exceptions caught during processing will be |
||
308 | * rethrown wrapped into a PropelException. |
||
309 | * @return array (BannerImageI18n object, last column rank) |
||
310 | */ |
||
311 | public static function populateObject($row, $offset = 0, $indexType = TableMap::TYPE_NUM) |
||
312 | { |
||
313 | $key = BannerImageI18nTableMap::getPrimaryKeyHashFromRow($row, $offset, $indexType); |
||
314 | if (null !== ($obj = BannerImageI18nTableMap::getInstanceFromPool($key))) { |
||
315 | // We no longer rehydrate the object, since this can cause data loss. |
||
316 | // See http://www.propelorm.org/ticket/509 |
||
317 | // $obj->hydrate($row, $offset, true); // rehydrate |
||
318 | $col = $offset + BannerImageI18nTableMap::NUM_HYDRATE_COLUMNS; |
||
319 | } else { |
||
320 | $cls = BannerImageI18nTableMap::OM_CLASS; |
||
321 | /** @var BannerImageI18n $obj */ |
||
322 | $obj = new $cls(); |
||
323 | $col = $obj->hydrate($row, $offset, false, $indexType); |
||
324 | BannerImageI18nTableMap::addInstanceToPool($obj, $key); |
||
325 | } |
||
326 | |||
327 | return array($obj, $col); |
||
328 | } |
||
329 | |||
330 | /** |
||
331 | * The returned array will contain objects of the default type or |
||
332 | * objects that inherit from the default. |
||
333 | * |
||
334 | * @param DataFetcherInterface $dataFetcher |
||
335 | * @return array |
||
336 | * @throws PropelException Any exceptions caught during processing will be |
||
337 | * rethrown wrapped into a PropelException. |
||
338 | */ |
||
339 | public static function populateObjects(DataFetcherInterface $dataFetcher) |
||
340 | { |
||
341 | $results = array(); |
||
342 | |||
343 | // set the class once to avoid overhead in the loop |
||
344 | $cls = static::getOMClass(false); |
||
345 | // populate the object(s) |
||
346 | while ($row = $dataFetcher->fetch()) { |
||
347 | $key = BannerImageI18nTableMap::getPrimaryKeyHashFromRow($row, 0, $dataFetcher->getIndexType()); |
||
348 | if (null !== ($obj = BannerImageI18nTableMap::getInstanceFromPool($key))) { |
||
349 | // We no longer rehydrate the object, since this can cause data loss. |
||
350 | // See http://www.propelorm.org/ticket/509 |
||
351 | // $obj->hydrate($row, 0, true); // rehydrate |
||
352 | $results[] = $obj; |
||
353 | } else { |
||
354 | /** @var BannerImageI18n $obj */ |
||
355 | $obj = new $cls(); |
||
356 | $obj->hydrate($row); |
||
357 | $results[] = $obj; |
||
358 | BannerImageI18nTableMap::addInstanceToPool($obj, $key); |
||
359 | } // if key exists |
||
360 | } |
||
361 | |||
362 | return $results; |
||
363 | } |
||
364 | /** |
||
365 | * Add all the columns needed to create a new object. |
||
366 | * |
||
367 | * Note: any columns that were marked with lazyLoad="true" in the |
||
368 | * XML schema will not be added to the select list and only loaded |
||
369 | * on demand. |
||
370 | * |
||
371 | * @param Criteria $criteria object containing the columns to add. |
||
372 | * @param string $alias optional table alias |
||
373 | * @throws PropelException Any exceptions caught during processing will be |
||
374 | * rethrown wrapped into a PropelException. |
||
375 | */ |
||
376 | public static function addSelectColumns(Criteria $criteria, $alias = null) |
||
394 | |||
395 | /** |
||
396 | * Returns the TableMap related to this object. |
||
397 | * This method is not needed for general use but a specific application could have a need. |
||
398 | * @return TableMap |
||
399 | * @throws PropelException Any exceptions caught during processing will be |
||
400 | * rethrown wrapped into a PropelException. |
||
401 | */ |
||
402 | public static function getTableMap() |
||
406 | |||
407 | /** |
||
408 | * Add a TableMap instance to the database for this tableMap class. |
||
409 | */ |
||
410 | View Code Duplication | public static function buildTableMap() |
|
417 | |||
418 | /** |
||
419 | * Performs a DELETE on the database, given a BannerImageI18n or Criteria object OR a primary key value. |
||
420 | * |
||
421 | * @param mixed $values Criteria or BannerImageI18n object or primary key or array of primary keys |
||
422 | * which is used to create the DELETE statement |
||
423 | * @param ConnectionInterface $con the connection to use |
||
424 | * @return int The number of affected rows (if supported by underlying database driver). This includes CASCADE-related rows |
||
425 | * if supported by native driver or if emulated using Propel. |
||
426 | * @throws PropelException Any exceptions caught during processing will be |
||
427 | * rethrown wrapped into a PropelException. |
||
428 | */ |
||
429 | public static function doDelete($values, ConnectionInterface $con = null) |
||
430 | { |
||
431 | if (null === $con) { |
||
432 | $con = Propel::getServiceContainer()->getWriteConnection(BannerImageI18nTableMap::DATABASE_NAME); |
||
433 | } |
||
434 | |||
435 | if ($values instanceof Criteria) { |
||
436 | // rename for clarity |
||
437 | $criteria = $values; |
||
438 | } elseif ($values instanceof \xbanners\models\BannerImageI18n) { // it's a model object |
||
439 | // create criteria based on pk values |
||
440 | $criteria = $values->buildPkeyCriteria(); |
||
441 | } else { // it's a primary key, or an array of pks |
||
442 | $criteria = new Criteria(BannerImageI18nTableMap::DATABASE_NAME); |
||
443 | // primary key is composite; we therefore, expect |
||
444 | // the primary key passed to be an array of pkey values |
||
445 | if (count($values) == count($values, COUNT_RECURSIVE)) { |
||
446 | // array is not multi-dimensional |
||
447 | $values = array($values); |
||
448 | } |
||
449 | foreach ($values as $value) { |
||
450 | $criterion = $criteria->getNewCriterion(BannerImageI18nTableMap::COL_ID, $value[0]); |
||
451 | $criterion->addAnd($criteria->getNewCriterion(BannerImageI18nTableMap::COL_LOCALE, $value[1])); |
||
452 | $criteria->addOr($criterion); |
||
453 | } |
||
454 | } |
||
455 | |||
456 | $query = BannerImageI18nQuery::create()->mergeWith($criteria); |
||
457 | |||
458 | if ($values instanceof Criteria) { |
||
459 | BannerImageI18nTableMap::clearInstancePool(); |
||
460 | } elseif (!is_object($values)) { // it's a primary key, or an array of pks |
||
461 | foreach ((array) $values as $singleval) { |
||
462 | BannerImageI18nTableMap::removeInstanceFromPool($singleval); |
||
463 | } |
||
464 | } |
||
465 | |||
466 | return $query->delete($con); |
||
467 | } |
||
468 | |||
469 | /** |
||
470 | * Deletes all rows from the banner_image_i18n table. |
||
471 | * |
||
472 | * @param ConnectionInterface $con the connection to use |
||
473 | * @return int The number of affected rows (if supported by underlying database driver). |
||
474 | */ |
||
475 | public static function doDeleteAll(ConnectionInterface $con = null) |
||
479 | |||
480 | /** |
||
481 | * Performs an INSERT on the database, given a BannerImageI18n or Criteria object. |
||
482 | * |
||
483 | * @param mixed $criteria Criteria or BannerImageI18n object containing data that is used to create the INSERT statement. |
||
484 | * @param ConnectionInterface $con the ConnectionInterface connection to use |
||
485 | * @return mixed The new primary key. |
||
486 | * @throws PropelException Any exceptions caught during processing will be |
||
487 | * rethrown wrapped into a PropelException. |
||
488 | */ |
||
489 | public static function doInsert($criteria, ConnectionInterface $con = null) |
||
511 | |||
512 | } // BannerImageI18nTableMap |
||
513 | // This is the static code needed to register the TableMap for this table with the main Propel class. |
||
516 |