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

Keyword/KeywordStorage/Gateway/DoctrineStorage.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\FieldType\Keyword\KeywordStorage\Gateway;
8
9 use Doctrine\DBAL\Connection;
10 use eZ\Publish\Core\FieldType\Keyword\KeywordStorage\Gateway;
11 use eZ\Publish\SPI\Persistence\Content\Field;
12 use RuntimeException;
13
14 class DoctrineStorage extends Gateway
15 {
16 const KEYWORD_TABLE = 'ezkeyword';
17 const KEYWORD_ATTRIBUTE_LINK_TABLE = 'ezkeyword_attribute_link';
18
20 protected $connection;
21
22 public function __construct(Connection $connection)
23 {
24 $this->connection = $connection;
25 }
26
33 public function storeFieldData(Field $field, $contentTypeId)
34 {
35 if (empty($field->value->externalData) && !empty($field->id)) {
36 $this->deleteFieldData($field->id);
37
38 return;
39 }
40
41 $existingKeywordMap = $this->getExistingKeywords(
42 $field->value->externalData,
43 $contentTypeId
44 );
45
46 $this->deleteOldKeywordAssignments($field->id);
47
48 $this->assignKeywords(
49 $field->id,
50 $this->insertKeywords(
51 array_diff_key(
52 array_fill_keys($field->value->externalData, true),
53 $existingKeywordMap
54 ),
55 $contentTypeId
56 ) + $existingKeywordMap
57 );
58
59 $this->deleteOrphanedKeywords();
60 }
61
67 public function getFieldData(Field $field)
68 {
69 $field->value->externalData = $this->getAssignedKeywords($field->id);
70 }
71
79 public function getContentTypeId(Field $field)
80 {
81 return $this->loadContentTypeId($field->fieldDefinitionId);
82 }
83
89 public function deleteFieldData($fieldId)
90 {
91 $this->deleteOldKeywordAssignments($fieldId);
92 $this->deleteOrphanedKeywords();
93 }
94
102 protected function getAssignedKeywords($fieldId)
103 {
104 $query = $this->connection->createQueryBuilder();
105 $query
106 ->select($this->connection->quoteIdentifier('keyword'))
107 ->from($this->connection->quoteIdentifier(self::KEYWORD_TABLE), 'kwd')
108 ->innerJoin(
109 'kwd',
110 $this->connection->quoteIdentifier(self::KEYWORD_ATTRIBUTE_LINK_TABLE),
111 'attr',
112 $query->expr()->eq(
113 $this->connection->quoteIdentifier('kwd.id'),
114 $this->connection->quoteIdentifier('attr.keyword_id')
115 )
116 )
117 ->where(
118 $query->expr()->eq(
119 $this->connection->quoteIdentifier('attr.objectattribute_id'),
120 ':fieldId'
121 )
122 )
123 ->setParameter(':fieldId', $fieldId)
124 ;
125
126 $statement = $query->execute();
127
128 return $statement->fetchAll(\PDO::FETCH_COLUMN);
129 }
130
138 protected function loadContentTypeId($fieldDefinitionId)
139 {
140 $query = $this->connection->createQueryBuilder();
141 $query
142 ->select($this->connection->quoteIdentifier('contentclass_id'))
143 ->from($this->connection->quoteIdentifier('ezcontentclass_attribute'))
144 ->where(
145 $query->expr()->eq('id', ':fieldDefinitionId')
146 )
147 ->setParameter(':fieldDefinitionId', $fieldDefinitionId);
148
149 $statement = $query->execute();
150
151 $row = $statement->fetch(\PDO::FETCH_ASSOC);
152
153 if ($row === false) {
154 throw new RuntimeException(
155 sprintf(
156 'Content Type ID cannot be retrieved based on the field definition ID "%s"',
157 $fieldDefinitionId
158 )
159 );
160 }
161
162 return (int) $row['contentclass_id'];
163 }
164
181 protected function getExistingKeywords($keywordList, $contentTypeId)
182 {
183 // Retrieving potentially existing keywords
184 $query = $this->connection->createQueryBuilder();
185 $query
186 ->select(
187 $this->connection->quoteIdentifier('id'),
188 $this->connection->quoteIdentifier('keyword')
189 )
190 ->from($this->connection->quoteIdentifier(self::KEYWORD_TABLE))
191 ->where(
192 $query->expr()->andX(
193 $query->expr()->in(
194 $this->connection->quoteIdentifier('keyword'),
195 ':keywordList'
196 ),
197 $query->expr()->eq(
198 $this->connection->quoteIdentifier('class_id'),
199 ':contentTypeId'
200 )
201 )
202 )
203 ->setParameter(':keywordList', $keywordList, Connection::PARAM_STR_ARRAY)
204 ->setParameter(':contentTypeId', $contentTypeId);
205
206 $statement = $query->execute();
207
208 $existingKeywordMap = [];
209 foreach ($statement->fetchAll(\PDO::FETCH_ASSOC) as $row) {
210 // filter out keywords that aren't the exact match (e.g. differ by case)
211 if (!in_array($row['keyword'], $keywordList)) {
212 continue;
213 }
214 $existingKeywordMap[$row['keyword']] = $row['id'];
215 }
216
217 return $existingKeywordMap;
218 }
219
237 protected function insertKeywords(array $keywordsToInsert, $contentTypeId)
238 {
239 $keywordIdMap = [];
240 // Inserting keywords not yet registered
241 if (!empty($keywordsToInsert)) {
242 $insertQuery = $this->connection->createQueryBuilder();
243 $insertQuery
244 ->insert($this->connection->quoteIdentifier(self::KEYWORD_TABLE))
245 ->values(
246 [
247 $this->connection->quoteIdentifier('class_id') => ':contentTypeId',
248 $this->connection->quoteIdentifier('keyword') => ':keyword',
249 ]
250 )
251 ->setParameter(':contentTypeId', $contentTypeId, \PDO::PARAM_INT);
252
253 foreach (array_keys($keywordsToInsert) as $keyword) {
254 $insertQuery->setParameter(':keyword', $keyword);
255 $insertQuery->execute();
256 $keywordIdMap[$keyword] = (int)$this->connection->lastInsertId(
257 $this->getSequenceName(self::KEYWORD_TABLE, 'id')
258 );
259 }
260 }
261
262 return $keywordIdMap;
263 }
264
265 protected function deleteOldKeywordAssignments($fieldId)
266 {
267 $deleteQuery = $this->connection->createQueryBuilder();
268 $deleteQuery
269 ->delete($this->connection->quoteIdentifier(self::KEYWORD_ATTRIBUTE_LINK_TABLE))
270 ->where(
271 $deleteQuery->expr()->eq(
272 $this->connection->quoteIdentifier('objectattribute_id'),
273 ':fieldId'
274 )
275 )
276 ->setParameter(':fieldId', $fieldId, \PDO::PARAM_INT);
277
278 $deleteQuery->execute();
279 }
280
295 protected function assignKeywords($fieldId, array $keywordMap)
296 {
297 $insertQuery = $this->connection->createQueryBuilder();
298 $insertQuery
299 ->insert($this->connection->quoteIdentifier(self::KEYWORD_ATTRIBUTE_LINK_TABLE))
300 ->values(
301 [
302 $this->connection->quoteIdentifier('keyword_id') => ':keywordId',
303 $this->connection->quoteIdentifier('objectattribute_id') => ':fieldId',
304 ]
305 )
306 ;
307
308 foreach ($keywordMap as $keyword => $keywordId) {
309 $insertQuery
310 ->setParameter(':keywordId', $keywordId, \PDO::PARAM_INT)
311 ->setParameter(':fieldId', $fieldId, \PDO::PARAM_INT);
312 $insertQuery->execute();
313 }
314 }
315
322 protected function deleteOrphanedKeywords()
323 {
324 $query = $this->connection->createQueryBuilder();
325 $query
326 ->select($this->connection->quoteIdentifier('kwd.id'))
327 ->from($this->connection->quoteIdentifier(self::KEYWORD_TABLE), 'kwd')
328 ->leftJoin(
329 'kwd',
330 $this->connection->quoteIdentifier(self::KEYWORD_ATTRIBUTE_LINK_TABLE),
331 'attr',
332 $query->expr()->eq(
333 $this->connection->quoteIdentifier('attr.keyword_id'),
334 $this->connection->quoteIdentifier('kwd.id')
335 )
336 )
337 ->where($query->expr()->isNull('attr.id'));
338
339 $statement = $query->execute();
340 $ids = $statement->fetchAll(\PDO::FETCH_COLUMN);
341
342 if (empty($ids)) {
343 return;
344 }
345
346 $deleteQuery = $this->connection->createQueryBuilder();
347 $deleteQuery
348 ->delete($this->connection->quoteIdentifier(self::KEYWORD_TABLE))
349 ->where(
350 $deleteQuery->expr()->in($this->connection->quoteIdentifier('id'), ':ids')
351 )
352 ->setParameter(':ids', $ids, Connection::PARAM_INT_ARRAY);
353
354 $deleteQuery->execute();
355 }
356 }
eZ\Publish\Core\FieldType\Keyword\KeywordStorage\Gateway\DoctrineStorage
Definition: Keyword/KeywordStorage/Gateway/DoctrineStorage.php:15
eZ\Publish\Core\FieldType\Keyword\KeywordStorage\Gateway\DoctrineStorage\getFieldData
getFieldData(Field $field)
Sets the list of assigned keywords into $field->value->externalData.
Definition: Keyword/KeywordStorage/Gateway/DoctrineStorage.php:67
eZ\Publish\Core\FieldType\Keyword\KeywordStorage\Gateway\DoctrineStorage\deleteOrphanedKeywords
deleteOrphanedKeywords()
Deletes all orphaned keywords.
Definition: Keyword/KeywordStorage/Gateway/DoctrineStorage.php:322
eZ\Publish\Core\FieldType\Keyword\KeywordStorage\Gateway\DoctrineStorage\storeFieldData
storeFieldData(Field $field, $contentTypeId)
Stores the keyword list from $field->value->externalData.
Definition: Keyword/KeywordStorage/Gateway/DoctrineStorage.php:33
eZ\Publish\Core\FieldType\Keyword\KeywordStorage\Gateway\DoctrineStorage\KEYWORD_TABLE
const KEYWORD_TABLE
Definition: Keyword/KeywordStorage/Gateway/DoctrineStorage.php:16
eZ\Publish\Core\FieldType\Keyword\KeywordStorage\Gateway\DoctrineStorage\$connection
$connection
Definition: Keyword/KeywordStorage/Gateway/DoctrineStorage.php:20
eZ\Publish\Core\FieldType\Keyword\KeywordStorage\Gateway\DoctrineStorage\getContentTypeId
getContentTypeId(Field $field)
Retrieve the ContentType ID for the given $field.
Definition: Keyword/KeywordStorage/Gateway/DoctrineStorage.php:79
eZ\Publish\Core\FieldType\Keyword\KeywordStorage\Gateway\DoctrineStorage\loadContentTypeId
loadContentTypeId($fieldDefinitionId)
Retrieves the content type ID for the given $fieldDefinitionId.
Definition: Keyword/KeywordStorage/Gateway/DoctrineStorage.php:138
eZ\Publish\Core\FieldType\Keyword\KeywordStorage\Gateway\DoctrineStorage\KEYWORD_ATTRIBUTE_LINK_TABLE
const KEYWORD_ATTRIBUTE_LINK_TABLE
Definition: Keyword/KeywordStorage/Gateway/DoctrineStorage.php:17
eZ\Publish\Core\FieldType\Keyword\KeywordStorage\Gateway\DoctrineStorage\assignKeywords
assignKeywords($fieldId, array $keywordMap)
Assigns keywords from $keywordMap to the field with $fieldId.
Definition: Keyword/KeywordStorage/Gateway/DoctrineStorage.php:295
eZ\Publish\Core\FieldType\Keyword\KeywordStorage\Gateway\DoctrineStorage\getExistingKeywords
getExistingKeywords($keywordList, $contentTypeId)
Returns already existing keywords from $keywordList as a map.
Definition: Keyword/KeywordStorage/Gateway/DoctrineStorage.php:181
eZ\Publish\Core\FieldType\Keyword\KeywordStorage\Gateway\DoctrineStorage\deleteFieldData
deleteFieldData($fieldId)
Deletes keyword data for the given $fieldId.
Definition: Keyword/KeywordStorage/Gateway/DoctrineStorage.php:89
eZ\Publish\Core\FieldType\Keyword\KeywordStorage\Gateway\DoctrineStorage\insertKeywords
insertKeywords(array $keywordsToInsert, $contentTypeId)
Inserts $keywordsToInsert for $fieldDefinitionId and returns a map of these keywords to their ID.
Definition: Keyword/KeywordStorage/Gateway/DoctrineStorage.php:237
eZ\Publish\Core\FieldType\Keyword\KeywordStorage\Gateway\DoctrineStorage\getAssignedKeywords
getAssignedKeywords($fieldId)
Returns a list of keywords assigned to $fieldId.
Definition: Keyword/KeywordStorage/Gateway/DoctrineStorage.php:102
eZ\Publish\Core\FieldType\Keyword\KeywordStorage\Gateway\DoctrineStorage\__construct
__construct(Connection $connection)
Definition: Keyword/KeywordStorage/Gateway/DoctrineStorage.php:22
eZ\Publish\Core\FieldType\Keyword\KeywordStorage\Gateway\DoctrineStorage\deleteOldKeywordAssignments
deleteOldKeywordAssignments($fieldId)
Definition: Keyword/KeywordStorage/Gateway/DoctrineStorage.php:265
eZ\Publish\Core\FieldType\Keyword\KeywordStorage\Gateway
Keyword Field Type external storage gateway.
Definition: FieldType/Keyword/KeywordStorage/Gateway.php:16
eZ\Publish\SPI\Persistence\Content\Field
Definition: SPI/Persistence/Content/Field.php:12
eZ\Publish\Core\FieldType\Keyword\KeywordStorage\Gateway
Definition: Keyword/KeywordStorage/Gateway/DoctrineStorage.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.