Skip to content

Commit 54e2918

Browse files
committed
IBX-7579:Richtext: Rows are added to ezurl_object_link on every save
1 parent 7e47231 commit 54e2918

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

eZ/Publish/Core/FieldType/Tests/Url/Gateway/DoctrineStorageTest.php

+23
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,29 @@ public function testUnlinkUrl()
194194
$this->assertEquals($expected, $result);
195195
}
196196

197+
/**
198+
* @covers \eZ\Publish\Core\FieldType\Url\UrlStorage\Gateway\DoctrineStorage::getUrlsFromUrlLink
199+
*/
200+
public function testGetUrlsFromUrlLink()
201+
{
202+
$gateway = $this->getStorageGateway();
203+
204+
$urlIds = [];
205+
$urlIds[] = $gateway->insertUrl('https://ibexa.co/example1');
206+
$urlIds[] = $gateway->insertUrl('https://ibexa.co/example2');
207+
$urlIds[] = $gateway->insertUrl('https://ibexa.co/example3');
208+
209+
$gateway->linkUrl($urlIds[0], 10, 1);
210+
$gateway->linkUrl($urlIds[1], 10, 1);
211+
$gateway->linkUrl($urlIds[1], 12, 2);
212+
$gateway->linkUrl($urlIds[2], 14, 1);
213+
214+
self::assertEquals(['https://ibexa.co/example1' => true, 'https://ibexa.co/example2' => true], $gateway->getUrlsFromUrlLink(10, 1), 'Did not get expected urlS for field 10');
215+
self::assertEquals(['https://ibexa.co/example2' => true], $gateway->getUrlsFromUrlLink(12, 2), 'Did not get expected url for field 12');
216+
self::assertEquals(['https://ibexa.co/example3' => true], $gateway->getUrlsFromUrlLink(14, 1), 'Did not get expected url for field 14');
217+
self::assertEquals([], $gateway->getUrlsFromUrlLink(15, 1), 'Expected no urls for field 15');
218+
}
219+
197220
protected function getStorageGateway(): Gateway
198221
{
199222
if (!isset($this->storageGateway)) {

eZ/Publish/Core/FieldType/Url/UrlStorage/Gateway.php

+7
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ abstract public function getUrlIdMap(array $urls);
4545
*/
4646
abstract public function insertUrl($url);
4747

48+
/**
49+
* Return a list of URLs used by the given field and version.
50+
*
51+
* @return bool[] An array of URLs, with urls as keys
52+
*/
53+
abstract public function getUrlsFromUrlLink(int $fieldId, int $versionNo): array;
54+
4855
/**
4956
* Creates link to URL with $urlId for field with $fieldId in $versionNo.
5057
*

eZ/Publish/Core/FieldType/Url/UrlStorage/Gateway/DoctrineStorage.php

+42
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,48 @@ public function insertUrl($url)
129129
);
130130
}
131131

132+
/**
133+
* Return a list of URLs used by the given field and version.
134+
*
135+
* @return bool[] An array of URLs, with urls as keys
136+
*/
137+
public function getUrlsFromUrlLink(int $fieldId, int $versionNo): array
138+
{
139+
$selectQuery = $this->connection->createQueryBuilder();
140+
$selectQuery
141+
->select($this->connection->quoteIdentifier('url.url'))
142+
->from($this->connection->quoteIdentifier(self::URL_TABLE), 'url')
143+
->leftJoin(
144+
'url',
145+
$this->connection->quoteIdentifier(self::URL_LINK_TABLE),
146+
'link',
147+
'url.id = link.url_id'
148+
)
149+
->where(
150+
$selectQuery->expr()->eq(
151+
'link.contentobject_attribute_id',
152+
':contentobject_attribute_id'
153+
)
154+
)
155+
->andWhere(
156+
$selectQuery->expr()->eq(
157+
'link.contentobject_attribute_version',
158+
':contentobject_attribute_version'
159+
)
160+
)
161+
->setParameter(':contentobject_attribute_id', $fieldId, ParameterType::INTEGER)
162+
->setParameter(':contentobject_attribute_version', $versionNo, ParameterType::INTEGER);
163+
164+
$statement = $selectQuery->execute();
165+
$rows = $statement->fetchAllAssociativeIndexed();
166+
$result = [];
167+
foreach ($rows as $url => $item) {
168+
$result[$url] = true;
169+
}
170+
171+
return $result;
172+
}
173+
132174
/**
133175
* Create link to URL with $urlId for field with $fieldId in $versionNo.
134176
*

0 commit comments

Comments
 (0)