1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace Artprima\QueryFilterBundle\Query; |
4
|
|
|
|
5
|
|
|
use Artprima\QueryFilterBundle\Exception\InvalidArgumentException; |
6
|
|
|
use Doctrine\ORM\EntityManager; |
7
|
|
|
use Doctrine\ORM\QueryBuilder; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class NeighborRecords |
11
|
|
|
* |
12
|
|
|
* @author Denis Voytyuk <[email protected]> |
13
|
|
|
* |
14
|
|
|
* @package Artprima\QueryFilterBundle\Query |
15
|
|
|
*/ |
16
|
|
|
class NeighborRecords |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private $primaryKeyColumn; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $rootEntity; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var EntityManager |
30
|
|
|
*/ |
31
|
|
|
private $entityManager; |
32
|
|
|
|
33
|
1 |
|
public function __construct(string $from, EntityManager $entityManager, string $primaryKeyColumn = 'c.id') |
34
|
|
|
{ |
35
|
1 |
|
$this->rootEntity = $from; |
36
|
1 |
|
$this->entityManager = $entityManager; |
37
|
1 |
|
$this->primaryKeyColumn = $primaryKeyColumn; |
38
|
1 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get neighbor (prev or next) record id for use in navigation |
42
|
|
|
* |
43
|
|
|
* @param int $id record id |
44
|
|
|
* @param boolean $prev if true - get prev id, otherwise - next id |
45
|
|
|
* @return QueryBuilder |
46
|
|
|
*/ |
47
|
1 |
|
public function getQueryBuilderFilteredByNeighborRecord(int $id, bool $prev): QueryBuilder |
48
|
|
|
{ |
49
|
1 |
|
$sign = $prev ? '<' : '>'; |
50
|
1 |
|
$order = $prev ? 'DESC' : 'ASC'; |
51
|
|
|
|
52
|
1 |
|
$qb = new QueryBuilder($this->entityManager); |
53
|
|
|
$qb |
54
|
1 |
|
->select($this->primaryKeyColumn) |
55
|
1 |
|
->from($this->rootEntity, 'c') |
56
|
1 |
|
->where($this->primaryKeyColumn.' '.$sign.' :id') |
57
|
1 |
|
->setParameter(':id', $id) |
58
|
1 |
|
->orderBy($this->primaryKeyColumn, $order) |
59
|
|
|
; |
60
|
|
|
|
61
|
1 |
|
return $qb; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|