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 UserQuery 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 UserQuery, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
92 | abstract class UserQuery extends ModelCriteria |
||
93 | { |
||
94 | protected $entityNotFoundExceptionClass = '\\Propel\\Runtime\\Exception\\EntityNotFoundException'; |
||
95 | |||
96 | /** |
||
97 | * Initializes internal state of \Jalle19\StatusManager\Database\Base\UserQuery 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\\User', $modelAlias = null) |
||
104 | { |
||
105 | parent::__construct($dbName, $modelName, $modelAlias); |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Returns a new ChildUserQuery 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 ChildUserQuery |
||
115 | */ |
||
116 | public static function create($modelAlias = null, Criteria $criteria = null) |
||
117 | { |
||
118 | if ($criteria instanceof ChildUserQuery) { |
||
119 | return $criteria; |
||
120 | } |
||
121 | $query = new ChildUserQuery(); |
||
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 ChildUser|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 = UserTableMap::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(UserTableMap::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 ChildUser A model object, or null if the key is not found |
||
178 | */ |
||
179 | protected function findPkSimple($key, ConnectionInterface $con) |
||
180 | { |
||
181 | $sql = 'SELECT id, instance_name, name FROM user WHERE id = :p0'; |
||
182 | try { |
||
183 | $stmt = $con->prepare($sql); |
||
184 | $stmt->bindValue(':p0', $key, PDO::PARAM_INT); |
||
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 ChildUser $obj */ |
||
193 | $obj = new ChildUser(); |
||
194 | $obj->hydrate($row); |
||
195 | UserTableMap::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 ChildUser|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|ChildUserQuery The current query, for fluid interface |
||
251 | */ |
||
252 | public function filterByPrimaryKey($key) |
||
253 | { |
||
254 | |||
255 | return $this->addUsingAlias(UserTableMap::COL_ID, $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|ChildUserQuery The current query, for fluid interface |
||
264 | */ |
||
265 | public function filterByPrimaryKeys($keys) |
||
266 | { |
||
267 | |||
268 | return $this->addUsingAlias(UserTableMap::COL_ID, $keys, Criteria::IN); |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * Filter the query on the id column |
||
273 | * |
||
274 | * Example usage: |
||
275 | * <code> |
||
276 | * $query->filterById(1234); // WHERE id = 1234 |
||
277 | * $query->filterById(array(12, 34)); // WHERE id IN (12, 34) |
||
278 | * $query->filterById(array('min' => 12)); // WHERE id > 12 |
||
279 | * </code> |
||
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|ChildUserQuery The current query, for fluid interface |
||
288 | */ |
||
289 | public function filterById($id = null, $comparison = null) |
||
290 | { |
||
291 | if (is_array($id)) { |
||
292 | $useMinMax = false; |
||
293 | if (isset($id['min'])) { |
||
294 | $this->addUsingAlias(UserTableMap::COL_ID, $id['min'], Criteria::GREATER_EQUAL); |
||
295 | $useMinMax = true; |
||
296 | } |
||
297 | if (isset($id['max'])) { |
||
298 | $this->addUsingAlias(UserTableMap::COL_ID, $id['max'], Criteria::LESS_EQUAL); |
||
299 | $useMinMax = true; |
||
300 | } |
||
301 | if ($useMinMax) { |
||
302 | return $this; |
||
303 | } |
||
304 | if (null === $comparison) { |
||
305 | $comparison = Criteria::IN; |
||
306 | } |
||
307 | } |
||
308 | |||
309 | return $this->addUsingAlias(UserTableMap::COL_ID, $id, $comparison); |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * Filter the query on the instance_name column |
||
314 | * |
||
315 | * Example usage: |
||
316 | * <code> |
||
317 | * $query->filterByInstanceName('fooValue'); // WHERE instance_name = 'fooValue' |
||
318 | * $query->filterByInstanceName('%fooValue%'); // WHERE instance_name LIKE '%fooValue%' |
||
319 | * </code> |
||
320 | * |
||
321 | * @param string $instanceName 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|ChildUserQuery The current query, for fluid interface |
||
326 | */ |
||
327 | public function filterByInstanceName($instanceName = null, $comparison = null) |
||
328 | { |
||
329 | if (null === $comparison) { |
||
330 | if (is_array($instanceName)) { |
||
331 | $comparison = Criteria::IN; |
||
332 | } elseif (preg_match('/[\%\*]/', $instanceName)) { |
||
333 | $instanceName = str_replace('*', '%', $instanceName); |
||
334 | $comparison = Criteria::LIKE; |
||
335 | } |
||
336 | } |
||
337 | |||
338 | return $this->addUsingAlias(UserTableMap::COL_INSTANCE_NAME, $instanceName, $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|ChildUserQuery The current query, for fluid interface |
||
355 | */ |
||
356 | 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(UserTableMap::COL_NAME, $name, $comparison); |
||
368 | } |
||
369 | |||
370 | /** |
||
371 | * Filter the query by a related \Jalle19\StatusManager\Database\Instance object |
||
372 | * |
||
373 | * @param \Jalle19\StatusManager\Database\Instance|ObjectCollection $instance 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 ChildUserQuery The current query, for fluid interface |
||
379 | */ |
||
380 | public function filterByInstance($instance, $comparison = null) |
||
381 | { |
||
382 | if ($instance instanceof \Jalle19\StatusManager\Database\Instance) { |
||
383 | return $this |
||
384 | ->addUsingAlias(UserTableMap::COL_INSTANCE_NAME, $instance->getName(), $comparison); |
||
385 | } elseif ($instance instanceof ObjectCollection) { |
||
386 | if (null === $comparison) { |
||
387 | $comparison = Criteria::IN; |
||
388 | } |
||
389 | |||
390 | return $this |
||
391 | ->addUsingAlias(UserTableMap::COL_INSTANCE_NAME, $instance->toKeyValue('PrimaryKey', 'Name'), $comparison); |
||
392 | } else { |
||
393 | throw new PropelException('filterByInstance() only accepts arguments of type \Jalle19\StatusManager\Database\Instance or Collection'); |
||
394 | } |
||
395 | } |
||
396 | |||
397 | /** |
||
398 | * Adds a JOIN clause to the query using the Instance 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|ChildUserQuery The current query, for fluid interface |
||
404 | */ |
||
405 | public function joinInstance($relationAlias = null, $joinType = Criteria::INNER_JOIN) |
||
406 | { |
||
407 | $tableMap = $this->getTableMap(); |
||
408 | $relationMap = $tableMap->getRelation('Instance'); |
||
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, 'Instance'); |
||
424 | } |
||
425 | |||
426 | return $this; |
||
427 | } |
||
428 | |||
429 | /** |
||
430 | * Use the Instance relation Instance 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 \Jalle19\StatusManager\Database\InstanceQuery A secondary query class using the current class as primary query |
||
439 | */ |
||
440 | public function useInstanceQuery($relationAlias = null, $joinType = Criteria::INNER_JOIN) |
||
441 | { |
||
442 | return $this |
||
443 | ->joinInstance($relationAlias, $joinType) |
||
444 | ->useQuery($relationAlias ? $relationAlias : 'Instance', '\Jalle19\StatusManager\Database\InstanceQuery'); |
||
445 | } |
||
446 | |||
447 | /** |
||
448 | * Filter the query by a related \Jalle19\StatusManager\Database\Connection object |
||
449 | * |
||
450 | * @param \Jalle19\StatusManager\Database\Connection|ObjectCollection $connection the related object to use as filter |
||
451 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
452 | * |
||
453 | * @return ChildUserQuery The current query, for fluid interface |
||
454 | */ |
||
455 | public function filterByConnection($connection, $comparison = null) |
||
456 | { |
||
457 | if ($connection instanceof \Jalle19\StatusManager\Database\Connection) { |
||
458 | return $this |
||
459 | ->addUsingAlias(UserTableMap::COL_ID, $connection->getUserId(), $comparison); |
||
460 | } elseif ($connection instanceof ObjectCollection) { |
||
461 | return $this |
||
462 | ->useConnectionQuery() |
||
463 | ->filterByPrimaryKeys($connection->getPrimaryKeys()) |
||
464 | ->endUse(); |
||
465 | } else { |
||
466 | throw new PropelException('filterByConnection() only accepts arguments of type \Jalle19\StatusManager\Database\Connection or Collection'); |
||
467 | } |
||
468 | } |
||
469 | |||
470 | /** |
||
471 | * Adds a JOIN clause to the query using the Connection relation |
||
472 | * |
||
473 | * @param string $relationAlias optional alias for the relation |
||
474 | * @param string $joinType Accepted values are null, 'left join', 'right join', 'inner join' |
||
475 | * |
||
476 | * @return $this|ChildUserQuery The current query, for fluid interface |
||
477 | */ |
||
478 | public function joinConnection($relationAlias = null, $joinType = Criteria::LEFT_JOIN) |
||
479 | { |
||
480 | $tableMap = $this->getTableMap(); |
||
481 | $relationMap = $tableMap->getRelation('Connection'); |
||
482 | |||
483 | // create a ModelJoin object for this join |
||
484 | $join = new ModelJoin(); |
||
485 | $join->setJoinType($joinType); |
||
486 | $join->setRelationMap($relationMap, $this->useAliasInSQL ? $this->getModelAlias() : null, $relationAlias); |
||
487 | if ($previousJoin = $this->getPreviousJoin()) { |
||
488 | $join->setPreviousJoin($previousJoin); |
||
489 | } |
||
490 | |||
491 | // add the ModelJoin to the current object |
||
492 | if ($relationAlias) { |
||
493 | $this->addAlias($relationAlias, $relationMap->getRightTable()->getName()); |
||
494 | $this->addJoinObject($join, $relationAlias); |
||
495 | } else { |
||
496 | $this->addJoinObject($join, 'Connection'); |
||
497 | } |
||
498 | |||
499 | return $this; |
||
500 | } |
||
501 | |||
502 | /** |
||
503 | * Use the Connection relation Connection object |
||
504 | * |
||
505 | * @see useQuery() |
||
506 | * |
||
507 | * @param string $relationAlias optional alias for the relation, |
||
508 | * to be used as main alias in the secondary query |
||
509 | * @param string $joinType Accepted values are null, 'left join', 'right join', 'inner join' |
||
510 | * |
||
511 | * @return \Jalle19\StatusManager\Database\ConnectionQuery A secondary query class using the current class as primary query |
||
512 | */ |
||
513 | public function useConnectionQuery($relationAlias = null, $joinType = Criteria::LEFT_JOIN) |
||
514 | { |
||
515 | return $this |
||
516 | ->joinConnection($relationAlias, $joinType) |
||
517 | ->useQuery($relationAlias ? $relationAlias : 'Connection', '\Jalle19\StatusManager\Database\ConnectionQuery'); |
||
518 | } |
||
519 | |||
520 | /** |
||
521 | * Filter the query by a related \Jalle19\StatusManager\Database\Subscription object |
||
522 | * |
||
523 | * @param \Jalle19\StatusManager\Database\Subscription|ObjectCollection $subscription the related object to use as filter |
||
524 | * @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL |
||
525 | * |
||
526 | * @return ChildUserQuery The current query, for fluid interface |
||
527 | */ |
||
528 | public function filterBySubscription($subscription, $comparison = null) |
||
529 | { |
||
530 | if ($subscription instanceof \Jalle19\StatusManager\Database\Subscription) { |
||
531 | return $this |
||
532 | ->addUsingAlias(UserTableMap::COL_ID, $subscription->getUserId(), $comparison); |
||
533 | } elseif ($subscription instanceof ObjectCollection) { |
||
534 | return $this |
||
535 | ->useSubscriptionQuery() |
||
536 | ->filterByPrimaryKeys($subscription->getPrimaryKeys()) |
||
537 | ->endUse(); |
||
538 | } else { |
||
539 | throw new PropelException('filterBySubscription() only accepts arguments of type \Jalle19\StatusManager\Database\Subscription or Collection'); |
||
540 | } |
||
541 | } |
||
542 | |||
543 | /** |
||
544 | * Adds a JOIN clause to the query using the Subscription relation |
||
545 | * |
||
546 | * @param string $relationAlias optional alias for the relation |
||
547 | * @param string $joinType Accepted values are null, 'left join', 'right join', 'inner join' |
||
548 | * |
||
549 | * @return $this|ChildUserQuery The current query, for fluid interface |
||
550 | */ |
||
551 | public function joinSubscription($relationAlias = null, $joinType = Criteria::LEFT_JOIN) |
||
552 | { |
||
553 | $tableMap = $this->getTableMap(); |
||
554 | $relationMap = $tableMap->getRelation('Subscription'); |
||
555 | |||
556 | // create a ModelJoin object for this join |
||
557 | $join = new ModelJoin(); |
||
558 | $join->setJoinType($joinType); |
||
559 | $join->setRelationMap($relationMap, $this->useAliasInSQL ? $this->getModelAlias() : null, $relationAlias); |
||
560 | if ($previousJoin = $this->getPreviousJoin()) { |
||
561 | $join->setPreviousJoin($previousJoin); |
||
562 | } |
||
563 | |||
564 | // add the ModelJoin to the current object |
||
565 | if ($relationAlias) { |
||
566 | $this->addAlias($relationAlias, $relationMap->getRightTable()->getName()); |
||
567 | $this->addJoinObject($join, $relationAlias); |
||
568 | } else { |
||
569 | $this->addJoinObject($join, 'Subscription'); |
||
570 | } |
||
571 | |||
572 | return $this; |
||
573 | } |
||
574 | |||
575 | /** |
||
576 | * Use the Subscription relation Subscription object |
||
577 | * |
||
578 | * @see useQuery() |
||
579 | * |
||
580 | * @param string $relationAlias optional alias for the relation, |
||
581 | * to be used as main alias in the secondary query |
||
582 | * @param string $joinType Accepted values are null, 'left join', 'right join', 'inner join' |
||
583 | * |
||
584 | * @return \Jalle19\StatusManager\Database\SubscriptionQuery A secondary query class using the current class as primary query |
||
585 | */ |
||
586 | public function useSubscriptionQuery($relationAlias = null, $joinType = Criteria::LEFT_JOIN) |
||
587 | { |
||
588 | return $this |
||
589 | ->joinSubscription($relationAlias, $joinType) |
||
590 | ->useQuery($relationAlias ? $relationAlias : 'Subscription', '\Jalle19\StatusManager\Database\SubscriptionQuery'); |
||
591 | } |
||
592 | |||
593 | /** |
||
594 | * Exclude object from result |
||
595 | * |
||
596 | * @param ChildUser $user Object to remove from the list of results |
||
597 | * |
||
598 | * @return $this|ChildUserQuery The current query, for fluid interface |
||
599 | */ |
||
600 | public function prune($user = null) |
||
601 | { |
||
602 | if ($user) { |
||
603 | $this->addUsingAlias(UserTableMap::COL_ID, $user->getId(), Criteria::NOT_EQUAL); |
||
604 | } |
||
605 | |||
606 | return $this; |
||
607 | } |
||
608 | |||
609 | /** |
||
610 | * Deletes all rows from the user table. |
||
611 | * |
||
612 | * @param ConnectionInterface $con the connection to use |
||
613 | * @return int The number of affected rows (if supported by underlying database driver). |
||
614 | */ |
||
615 | public function doDeleteAll(ConnectionInterface $con = null) |
||
616 | { |
||
617 | if (null === $con) { |
||
618 | $con = Propel::getServiceContainer()->getWriteConnection(UserTableMap::DATABASE_NAME); |
||
619 | } |
||
620 | |||
621 | // use transaction because $criteria could contain info |
||
622 | // for more than one table or we could emulating ON DELETE CASCADE, etc. |
||
623 | return $con->transaction(function () use ($con) { |
||
624 | $affectedRows = 0; // initialize var to track total num of affected rows |
||
625 | $affectedRows += parent::doDeleteAll($con); |
||
626 | // Because this db requires some delete cascade/set null emulation, we have to |
||
627 | // clear the cached instance *after* the emulation has happened (since |
||
628 | // instances get re-added by the select statement contained therein). |
||
629 | UserTableMap::clearInstancePool(); |
||
630 | UserTableMap::clearRelatedInstancePool(); |
||
631 | |||
632 | return $affectedRows; |
||
633 | }); |
||
634 | } |
||
635 | |||
636 | /** |
||
637 | * Performs a DELETE on the database based on the current ModelCriteria |
||
638 | * |
||
639 | * @param ConnectionInterface $con the connection to use |
||
640 | * @return int The number of affected rows (if supported by underlying database driver). This includes CASCADE-related rows |
||
641 | * if supported by native driver or if emulated using Propel. |
||
642 | * @throws PropelException Any exceptions caught during processing will be |
||
643 | * rethrown wrapped into a PropelException. |
||
644 | */ |
||
645 | public function delete(ConnectionInterface $con = null) |
||
646 | { |
||
647 | if (null === $con) { |
||
648 | $con = Propel::getServiceContainer()->getWriteConnection(UserTableMap::DATABASE_NAME); |
||
649 | } |
||
650 | |||
651 | $criteria = $this; |
||
652 | |||
653 | // Set the correct dbName |
||
654 | $criteria->setDbName(UserTableMap::DATABASE_NAME); |
||
655 | |||
656 | // use transaction because $criteria could contain info |
||
657 | // for more than one table or we could emulating ON DELETE CASCADE, etc. |
||
658 | return $con->transaction(function () use ($con, $criteria) { |
||
659 | $affectedRows = 0; // initialize var to track total num of affected rows |
||
660 | |||
661 | UserTableMap::removeInstanceFromPool($criteria); |
||
662 | |||
663 | $affectedRows += ModelCriteria::delete($con); |
||
664 | UserTableMap::clearRelatedInstancePool(); |
||
665 | |||
666 | return $affectedRows; |
||
667 | }); |
||
668 | } |
||
669 | |||
670 | } // UserQuery |
||
671 |