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