Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

SelectDoctrineQuery.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\Persistence\Doctrine;
8
9 use eZ\Publish\Core\Persistence\Database\SelectQuery;
10 use eZ\Publish\Core\Persistence\Database\QueryException;
11
18 class SelectDoctrineQuery extends AbstractDoctrineQuery implements SelectQuery
19 {
20 private $parts = [
21 'select' => [],
22 'from' => [],
23 'where' => [],
24 'orderBy' => [],
25 'groupBy' => [],
26 'having' => [],
27 ];
28
30 private $distinct = false;
31
33 private $limit;
34
36 private $offset;
37
49 public $permissionSubtreeJoinAdded = false;
50
84 public function select()
85 {
86 $args = $this->parseArguments(func_get_args());
87
88 foreach ($args as $selectPart) {
89 $this->parts['select'][] = $selectPart;
90 }
91
92 return $this;
93 }
94
113 public function alias($name, $alias)
114 {
115 return $name . ' ' . $alias;
116 }
117
156 public function selectDistinct()
157 {
158 $this->distinct = true;
159
160 return call_user_func_array([$this, 'select'], func_get_args());
161 }
162
185 public function from()
186 {
187 $args = $this->parseArguments(func_get_args());
188
189 foreach ($args as $tableName) {
190 $this->parts['from'][] = [
191 'table' => $tableName,
192 'type' => 'FROM',
193 ];
194 }
195
196 return $this;
197 }
198
250 public function innerJoin()
251 {
252 return $this->doJoin('INNER', func_get_args());
253 }
254
263 protected function doJoin($type, array $args)
264 {
265 if (count($args) === 0) {
266 throw new QueryException('No arguments given');
267 }
268
269 $tableName = $args[0];
270 $condition = '';
271
272 if (count($args) == 2) {
273 $condition = $args[1];
274 } elseif (count($args) == 3) {
275 $condition = $args[1] . ' = ' . $args[2];
276 }
277
278 $this->parts['from'][] = [
279 'table' => $tableName,
280 'type' => $type,
281 'condition' => $condition,
282 ];
283
284 return $this;
285 }
286
338 public function leftJoin()
339 {
340 return $this->doJoin('LEFT', func_get_args());
341 }
342
394 public function rightJoin()
395 {
396 return $this->doJoin('RIGHT', func_get_args());
397 }
398
421 public function where()
422 {
423 $args = func_get_args();
424
425 if (count($args) === 0) {
426 throw new QueryException('No arguments given');
427 }
428
429 foreach ($args as $whereCondition) {
430 $this->parts['where'][] = $whereCondition;
431 }
432
433 return $this;
434 }
435
458 public function limit($limit, $offset = '')
459 {
460 $this->limit = $limit;
461 $this->offset = $offset;
462 }
463
483 public function orderBy($column, $type = self::ASC)
484 {
485 if ($type !== self::ASC && $type !== self::DESC) {
486 throw new QueryException('Invalid value for type of order by orientation: ' . $type);
487 }
488
489 $this->parts['orderBy'][] = $column . ' ' . $type;
490
491 return $this;
492 }
493
512 public function groupBy()
513 {
514 $args = $this->parseArguments(func_get_args());
515
516 foreach ($args as $groupByExpression) {
517 $this->parts['groupBy'][] = $groupByExpression;
518 }
519
520 return $this;
521 }
522
543 public function having()
544 {
545 $args = func_get_args();
546
547 if (count($args) === 0) {
548 throw new QueryException('No arguments given');
549 }
550
551 foreach ($args as $whereCondition) {
552 $this->parts['having'][] = $whereCondition;
553 }
554
555 return $this;
556 }
557
565 public function getQuery()
566 {
567 if (count($this->parts['select']) === 0) {
568 throw new QueryException('Missing "select" parts to generate query.');
569 }
570
571 $sql = 'SELECT ';
572
573 if ($this->distinct) {
574 $sql .= 'DISTINCT ';
575 }
576
577 $sql .= implode(', ', $this->parts['select']) . ' FROM';
578
579 if (count($this->parts['from']) === 0) {
580 throw new QueryException('Missing "from" parts to generate query.');
581 }
582
583 $renderedFromBefore = false;
584
585 foreach ($this->parts['from'] as $fromPart) {
586 if ($fromPart['type'] === 'FROM') {
587 if ($renderedFromBefore === true) {
588 $sql .= ',';
589 }
590
591 $sql .= ' ' . $fromPart['table'];
592 $renderedFromBefore = true;
593 } else {
594 $sql .= ' ' . $fromPart['type'] . ' JOIN ' . $fromPart['table'];
595
596 if ($fromPart['condition']) {
597 $sql .= ' ON ' . $fromPart['condition'];
598 }
599 }
600 }
601
602 if (count($this->parts['where']) > 0) {
603 $sql .= ' WHERE ' . implode(' AND ', $this->parts['where']);
604 }
605
606 if (count($this->parts['groupBy']) > 0) {
607 $sql .= ' GROUP BY ' . implode(', ', $this->parts['groupBy']);
608 }
609
610 if (count($this->parts['having']) > 0) {
611 $sql .= ' HAVING ' . implode(' AND ', $this->parts['having']);
612 }
613
614 if (count($this->parts['orderBy']) > 0) {
615 $sql .= ' ORDER BY ' . implode(', ', $this->parts['orderBy']);
616 }
617
618 if ($this->limit || $this->offset) {
619 $sql = $this->connection->getDatabasePlatform()->modifyLimitQuery(
620 $sql,
621 $this->limit,
622 $this->offset
623 );
624 }
625
626 return $sql;
627 }
628 }
eZ\Publish\Core\Persistence\Database\QueryException
Class QueryException.
Definition: QueryException.php:18
eZ\Publish\Core\Persistence\Doctrine\AbstractDoctrineQuery
Class AbstractDoctrineQuery.
Definition: AbstractDoctrineQuery.php:21
eZ\Publish\Core\Persistence\Doctrine\AbstractDoctrineQuery\parseArguments
parseArguments(array $args)
Parse the arguments and validate for existance of values.
Definition: AbstractDoctrineQuery.php:212
eZ\Publish\Core\Persistence\Doctrine\SelectDoctrineQuery
Class SelectDoctrineQuery.
Definition: SelectDoctrineQuery.php:19
eZ\Publish\Core\Persistence\Doctrine\SelectDoctrineQuery\groupBy
groupBy()
Returns SQL that groups the result set by a given column.
Definition: SelectDoctrineQuery.php:512
eZ\Publish\Core\Persistence\Doctrine\SelectDoctrineQuery\$limit
$limit
Definition: SelectDoctrineQuery.php:33
eZ\Publish\Core\Persistence\Doctrine\SelectDoctrineQuery\where
where()
Adds a where clause with logical expressions to the query.
Definition: SelectDoctrineQuery.php:421
eZ\Publish\Core\Persistence\Doctrine\SelectDoctrineQuery\getQuery
getQuery()
Returns the query string for this query object.
Definition: SelectDoctrineQuery.php:565
eZ\Publish\Core\Persistence\Doctrine\SelectDoctrineQuery\$distinct
$distinct
Definition: SelectDoctrineQuery.php:30
eZ\Publish\Core\Persistence\Doctrine\SelectDoctrineQuery\innerJoin
innerJoin()
Returns the SQL for an inner join or prepares $fromString for an inner join.
Definition: SelectDoctrineQuery.php:250
eZ\Publish\Core\Persistence\Doctrine\SelectDoctrineQuery\leftJoin
leftJoin()
Returns the SQL for a left join or prepares $fromString for a left join.
Definition: SelectDoctrineQuery.php:338
eZ\Publish\Core\Persistence\Doctrine\SelectDoctrineQuery\$offset
$offset
Definition: SelectDoctrineQuery.php:36
eZ\Publish\Core\Persistence\Doctrine\SelectDoctrineQuery\from
from()
Select which tables you want to select from.
Definition: SelectDoctrineQuery.php:185
eZ\Publish\Core\Persistence\Doctrine\SelectDoctrineQuery\limit
limit($limit, $offset='')
Returns SQL that limits the result set.
Definition: SelectDoctrineQuery.php:458
eZ\Publish\Core\Persistence\Doctrine\SelectDoctrineQuery\doJoin
doJoin($type, array $args)
Helper function to generate join.
Definition: SelectDoctrineQuery.php:263
eZ\Publish\Core\Persistence\Doctrine\SelectDoctrineQuery\select
select()
Opens the query and selects which columns you want to return with the query.
Definition: SelectDoctrineQuery.php:84
eZ\Publish\Core\Persistence\Doctrine\SelectDoctrineQuery\having
having()
Returns SQL that set having by a given expression.
Definition: SelectDoctrineQuery.php:543
eZ\Publish\Core\Persistence\Doctrine\SelectDoctrineQuery\rightJoin
rightJoin()
Returns the SQL for a right join or prepares $fromString for a right join.
Definition: SelectDoctrineQuery.php:394
eZ\Publish\Core\Persistence\Doctrine\SelectDoctrineQuery\selectDistinct
selectDistinct()
Opens the query and uses a distinct select on the columns you want to return with the query.
Definition: SelectDoctrineQuery.php:156
eZ\Publish\Core\Persistence\Doctrine\SelectDoctrineQuery\alias
alias($name, $alias)
Returns SQL to create an alias.
Definition: SelectDoctrineQuery.php:113
eZ\Publish\Core\Persistence\Doctrine\SelectDoctrineQuery\orderBy
orderBy($column, $type=self::ASC)
Returns SQL that orders the result set by a given column.
Definition: SelectDoctrineQuery.php:483
eZ\Publish\Core\Persistence\Doctrine\SelectDoctrineQuery\$parts
$parts
Definition: SelectDoctrineQuery.php:20
eZ\Publish\Core\Persistence\Doctrine\SelectDoctrineQuery\$permissionSubtreeJoinAdded
$permissionSubtreeJoinAdded
Definition: SelectDoctrineQuery.php:49
eZ\Publish\Core\Persistence\Database\SelectQuery
Definition: SelectQuery.php:16
eZ\Publish\Core\Persistence\Doctrine
Definition: AbstractDoctrineQuery.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.