diff --git a/eZ/Publish/Core/FieldType/Url/UrlStorage/Gateway.php b/eZ/Publish/Core/FieldType/Url/UrlStorage/Gateway.php index abb0e96a7f..a492c618d1 100644 --- a/eZ/Publish/Core/FieldType/Url/UrlStorage/Gateway.php +++ b/eZ/Publish/Core/FieldType/Url/UrlStorage/Gateway.php @@ -45,6 +45,13 @@ abstract public function getUrlIdMap(array $urls); */ abstract public function insertUrl($url); + /** + * Return a list of URLs used by the given field and version. + * + * @return bool[] An array of URLs, with urls as keys + */ + abstract public function getUrlsFromUrlLink(int $fieldId, int $versionNo): array; + /** * Creates link to URL with $urlId for field with $fieldId in $versionNo. * diff --git a/eZ/Publish/Core/FieldType/Url/UrlStorage/Gateway/DoctrineStorage.php b/eZ/Publish/Core/FieldType/Url/UrlStorage/Gateway/DoctrineStorage.php index 4629095095..e627e69eb7 100644 --- a/eZ/Publish/Core/FieldType/Url/UrlStorage/Gateway/DoctrineStorage.php +++ b/eZ/Publish/Core/FieldType/Url/UrlStorage/Gateway/DoctrineStorage.php @@ -129,6 +129,44 @@ public function insertUrl($url) ); } + /** + * Return a list of URLs used by the given field and version. + * + * string[] An array of URLs + */ + public function getUrlsFromUrlLink(int $fieldId, int $versionNo): array + { + $selectQuery = $this->connection->createQueryBuilder(); + $selectQuery + ->select($this->connection->quoteIdentifier('url.url')) + ->from($this->connection->quoteIdentifier(self::URL_TABLE), 'url') + ->innerJoin( + 'url', + $this->connection->quoteIdentifier(self::URL_LINK_TABLE), + 'link', + 'url.id = link.url_id' + ) + ->where( + $selectQuery->expr()->eq( + 'link.contentobject_attribute_id', + ':contentobject_attribute_id' + ) + ) + ->andWhere( + $selectQuery->expr()->eq( + 'link.contentobject_attribute_version', + ':contentobject_attribute_version' + ) + ) + ->setParameter(':contentobject_attribute_id', $fieldId, ParameterType::INTEGER) + ->setParameter(':contentobject_attribute_version', $versionNo, ParameterType::INTEGER); + + $statement = $selectQuery->execute(); + $rows = $statement->fetchFirstColumn(); + + return $rows; + } + /** * Create link to URL with $urlId for field with $fieldId in $versionNo. * diff --git a/tests/integration/Core/FieldType/Url/UrlStorage/Gateway/UrlDoctrineStorageGatewayTest.php b/tests/integration/Core/FieldType/Url/UrlStorage/Gateway/UrlDoctrineStorageGatewayTest.php new file mode 100644 index 0000000000..9306139198 --- /dev/null +++ b/tests/integration/Core/FieldType/Url/UrlStorage/Gateway/UrlDoctrineStorageGatewayTest.php @@ -0,0 +1,46 @@ +getGateway(); + + $urlIds = []; + $urlIds[] = $gateway->insertUrl('https://ibexa.co/example1'); + $urlIds[] = $gateway->insertUrl('https://ibexa.co/example2'); + $urlIds[] = $gateway->insertUrl('https://ibexa.co/example3'); + + $gateway->linkUrl($urlIds[0], 10, 1); + $gateway->linkUrl($urlIds[1], 10, 1); + $gateway->linkUrl($urlIds[1], 12, 2); + $gateway->linkUrl($urlIds[2], 14, 1); + + $urls = $gateway->getUrlsFromUrlLink(10, 1); + sort($urls); + self::assertEquals(['https://ibexa.co/example1', 'https://ibexa.co/example2'], $urls, 'Did not get expected urls for field 10'); + self::assertEquals(['https://ibexa.co/example2'], $gateway->getUrlsFromUrlLink(12, 2), 'Did not get expected url for field 12'); + self::assertEquals(['https://ibexa.co/example3'], $gateway->getUrlsFromUrlLink(14, 1), 'Did not get expected url for field 14'); + self::assertEquals([], $gateway->getUrlsFromUrlLink(15, 1), 'Expected no urls for field 15'); + } + + protected function getGateway(): UrlStorageGateway + { + return new DoctrineStorage($this->getDatabaseConnection()); + } +}