1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Netgen\InformationCollection\Core\Persistence\Gateway; |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Connection; |
8
|
|
|
use PDO; |
9
|
|
|
|
10
|
|
|
final class DoctrineDatabase |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var \Doctrine\DBAL\Connection |
14
|
|
|
*/ |
15
|
|
|
protected $connection; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* DoctrineDatabase constructor. |
19
|
|
|
* |
20
|
|
|
* @param \Doctrine\DBAL\Connection $connection |
21
|
|
|
*/ |
22
|
|
|
public function __construct(Connection $connection) |
23
|
|
|
{ |
24
|
|
|
$this->connection = $connection; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Returns number of content objects that have any collection. |
29
|
|
|
* |
30
|
|
|
* @throws \Doctrine\DBAL\DBALException |
31
|
|
|
* |
32
|
|
|
* @return int |
33
|
|
|
*/ |
34
|
|
|
public function getContentsWithCollectionsCount(): int |
35
|
|
|
{ |
36
|
|
|
$query = $this->connection->createQueryBuilder(); |
37
|
|
|
$query->select( |
38
|
|
|
'COUNT(DISTINCT eic.contentobject_id) AS count' |
39
|
|
|
) |
40
|
|
|
->from($this->connection->quoteIdentifier('ezinfocollection'), 'eic') |
41
|
|
|
->innerJoin( |
42
|
|
|
'eic', |
43
|
|
|
$this->connection->quoteIdentifier('ezcontentobject'), |
44
|
|
|
'eco', |
45
|
|
|
$query->expr()->eq( |
46
|
|
|
$this->connection->quoteIdentifier('eic.contentobject_id'), |
47
|
|
|
$this->connection->quoteIdentifier('eco.id') |
48
|
|
|
) |
49
|
|
|
) |
50
|
|
|
->leftJoin( |
51
|
|
|
'eic', |
52
|
|
|
$this->connection->quoteIdentifier('ezcontentobject_tree'), |
53
|
|
|
'ecot', |
54
|
|
|
$query->expr()->eq( |
55
|
|
|
$this->connection->quoteIdentifier('eic.contentobject_id'), |
56
|
|
|
$this->connection->quoteIdentifier('ecot.contentobject_id') |
57
|
|
|
) |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
$statement = $query->execute(); |
61
|
|
|
|
62
|
|
|
return (int) $statement->fetchColumn(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Returns content objects with their collections. |
67
|
|
|
* |
68
|
|
|
* @param int $limit |
69
|
|
|
* @param int $offset |
70
|
|
|
* |
71
|
|
|
* @throws \Doctrine\DBAL\DBALException |
72
|
|
|
* |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
|
|
public function getObjectsWithCollections(int $limit, int $offset): array |
76
|
|
|
{ |
77
|
|
|
$contentIdsQuery = $this->connection->createQueryBuilder(); |
78
|
|
|
$contentIdsQuery |
79
|
|
|
->select('DISTINCT contentobject_id AS id') |
80
|
|
|
->from($this->connection->quoteIdentifier('ezinfocollection')); |
81
|
|
|
|
82
|
|
|
$statement = $contentIdsQuery->execute(); |
83
|
|
|
|
84
|
|
|
$contents = []; |
85
|
|
|
foreach ($statement->fetchAll() as $content) { |
86
|
|
|
$contents[] = (int) $content['id']; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if (empty($contents)) { |
90
|
|
|
return []; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$query = $this->connection->createQueryBuilder(); |
94
|
|
|
$query |
95
|
|
|
->select( |
96
|
|
|
'eco.id AS content_id', |
97
|
|
|
'ecot.main_node_id' |
98
|
|
|
) |
99
|
|
|
->from($this->connection->quoteIdentifier('ezcontentobject'), 'eco') |
100
|
|
|
->leftJoin( |
101
|
|
|
'eco', |
102
|
|
|
$this->connection->quoteIdentifier('ezcontentobject_tree'), |
103
|
|
|
'ecot', |
104
|
|
|
$query->expr()->eq( |
105
|
|
|
$this->connection->quoteIdentifier('eco.id'), |
106
|
|
|
$this->connection->quoteIdentifier('ecot.contentobject_id') |
107
|
|
|
) |
108
|
|
|
) |
109
|
|
|
->innerJoin( |
110
|
|
|
'eco', |
111
|
|
|
$this->connection->quoteIdentifier('ezcontentclass'), |
112
|
|
|
'ecc', |
113
|
|
|
$query->expr()->eq( |
114
|
|
|
$this->connection->quoteIdentifier('eco.contentclass_id'), |
115
|
|
|
$this->connection->quoteIdentifier('ecc.id') |
116
|
|
|
) |
117
|
|
|
) |
118
|
|
|
->where( |
119
|
|
|
$query->expr()->eq('ecc.version', 0) |
120
|
|
|
) |
121
|
|
|
->andWhere($query->expr()->in('eco.id', $contents)) |
122
|
|
|
->groupBy([ |
123
|
|
|
$this->connection->quoteIdentifier('ecot.main_node_id'), |
124
|
|
|
$this->connection->quoteIdentifier('content_id'), |
125
|
|
|
]) |
126
|
|
|
->setFirstResult($offset) |
127
|
|
|
->setMaxResults($limit); |
128
|
|
|
|
129
|
|
|
$statement = $query->execute(); |
130
|
|
|
|
131
|
|
|
return $statement->fetchAll(PDO::FETCH_ASSOC); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|