Skip to content

Commit

Permalink
Merge pull request #3295 from acrobat/fix-doctrine-deprecations
Browse files Browse the repository at this point in the history
[AllBundles] Fix doctrine dbal/orm deprecations
  • Loading branch information
acrobat authored Jun 17, 2023
2 parents ee194bf + 65109cd commit fd5fb9c
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 36 deletions.
10 changes: 4 additions & 6 deletions src/Kunstmaan/AdminBundle/Helper/Security/Acl/AclHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Kunstmaan\AdminBundle\Helper\Security\Acl;

use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\QuoteStrategy;
use Doctrine\ORM\Query;
Expand Down Expand Up @@ -167,20 +168,17 @@ private function getPermittedAclIdsSQLForUser(Query $query): string
}

$objectIdentifierColumn = 'o.object_identifier';
if ($databasePlatform->getName() === 'postgresql') {
if ($databasePlatform instanceof PostgreSQLPlatform) {
$objectIdentifierColumn .= '::BIGINT';
}

$selectQuery = <<<SELECTQUERY
SELECT DISTINCT {$objectIdentifierColumn} as id FROM acl_object_identities as o
INNER JOIN acl_classes c ON c.id = o.class_id
LEFT JOIN acl_entries e ON (
e.class_id = o.class_id AND (e.object_identity_id = o.id
OR {$databasePlatform->getIsNullExpression('e.object_identity_id')})
)
LEFT JOIN acl_security_identities s ON (
s.id = e.security_identity_id
e.class_id = o.class_id AND (e.object_identity_id = o.id OR e.object_identity_id IS NULL)
)
LEFT JOIN acl_security_identities s ON (s.id = e.security_identity_id)
WHERE c.class_type = {$rootEntity}
AND (s.identifier = {$inString})
AND e.mask & {$mask} > 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Kunstmaan\AdminBundle\Helper\Security\Acl;

use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Query\QueryBuilder;
use Doctrine\ORM\EntityManager;
use Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\MaskBuilder;
Expand Down Expand Up @@ -103,20 +104,17 @@ public function apply(QueryBuilder $queryBuilder, PermissionDefinition $permissi
}

$objectIdentifierColumn = 'o.object_identifier';
if ($databasePlatform->getName() === 'postgresql') {
if ($databasePlatform instanceof PostgreSQLPlatform) {
$objectIdentifierColumn .= '::BIGINT';
}

$joinTableQuery = <<<SELECTQUERY
SELECT DISTINCT {$objectIdentifierColumn} as id FROM acl_object_identities as o
INNER JOIN acl_classes c ON c.id = o.class_id
LEFT JOIN acl_entries e ON (
e.class_id = o.class_id AND (e.object_identity_id = o.id
OR {$databasePlatform->getIsNullExpression('e.object_identity_id')})
)
LEFT JOIN acl_security_identities s ON (
s.id = e.security_identity_id
e.class_id = o.class_id AND (e.object_identity_id = o.id OR e.object_identity_id IS NULL)
)
LEFT JOIN acl_security_identities s ON (s.id = e.security_identity_id)
WHERE c.class_type = {$rootEntity}
AND (s.identifier = {$inString})
AND e.mask & {$mask} > 0
Expand Down
15 changes: 6 additions & 9 deletions src/Kunstmaan/AdminBundle/Helper/Security/Acl/AclWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Kunstmaan\AdminBundle\Helper\Security\Acl;

use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\ORM\Query\SqlWalker;

/**
Expand All @@ -23,18 +24,14 @@ public function walkFromClause($fromClause)
$alias = $this->getQuery()->getHint('acl.entityRootTableDqlAlias');
$tableAlias = $this->getSQLTableAlias($name, $alias);
$extraQuery = $this->getQuery()->getHint('acl.extra.query');
$tempAclView = <<<tempAclView
JOIN ({$extraQuery}) ta_ ON {$tableAlias}.id = ta_.id
tempAclView;

switch ($this->getConnection()->getDatabasePlatform()->getName()) {
case 'postgresql':
$tempAclView = <<<tempAclView
if ($this->getConnection()->getDatabasePlatform() instanceof PostgreSQLPlatform) {
$tempAclView = <<<tempAclView
JOIN ({$extraQuery}) ta_ ON {$tableAlias}.id = ta_.id::integer
tempAclView;
break;
default:
$tempAclView = <<<tempAclView
JOIN ({$extraQuery}) ta_ ON {$tableAlias}.id = ta_.id
tempAclView;
break;
}

return $sql . ' ' . $tempAclView;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@ public function getItems()
/**
* Return an iterable statement or int for all items that matches the current filtering
*
* @return \Traversable|int
* @return iterable
*/
public function getIterator()
{
return $this->getQueryBuilder()->execute()->iterateAssociative();
return $this->getQueryBuilder()->executeQuery()->iterateAssociative();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,11 @@ public function getItems()
/**
* Return an iterator for all items that matches the current filtering
*
* @return \Iterator
* @return iterable
*/
public function getIterator()
{
return $this->getQuery()->iterate();
return $this->getQuery()->toIterable();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public function addExportField($name, $header);

public function buildIterator();

/** @return iterable */
public function getIterator();

public function buildFilters();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function buildExportFields()
}

/**
* @return \Iterator
* @return iterable
*/
public function getIterator()
{
Expand All @@ -120,7 +120,7 @@ public function buildIterator()
->setParameter('node', $this->nodeTranslation->getNode()->getId())
->setParameter('lang', $this->nodeTranslation->getLang())
->addOrderBy('fs.created', 'DESC');
$iterableResult = $qb->getQuery()->iterate();
$iterableResult = $qb->getQuery()->toIterable();
$isHeaderWritten = false;

$collection = new ArrayCollection();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ protected function getMockedEntityManager()
->getMock();

$query->expects($this->any())
->method('iterate')
->method('toIterable')
->willReturn($submissions);

$methods = [
Expand Down
21 changes: 14 additions & 7 deletions src/Kunstmaan/NodeBundle/Repository/NodeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Kunstmaan\NodeBundle\Repository;

use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Gedmo\Tree\Entity\Repository\NestedTreeRepository;
use Kunstmaan\AdminBundle\Entity\BaseUser;
use Kunstmaan\AdminBundle\Helper\Security\Acl\AclHelper;
Expand Down Expand Up @@ -266,20 +268,25 @@ public function getAllMenuNodes(
) {
$connection = $this->_em->getConnection();
$qb = $connection->createQueryBuilder();
$databasePlatformName = $connection->getDatabasePlatform()->getName();
$databasePlatform = $connection->getDatabasePlatform();
$createIfStatement = static function (
$expression,
$trueValue,
$falseValue
) use ($databasePlatformName) {
switch ($databasePlatformName) {
case 'sqlite':
) use ($databasePlatform) {
$defaultStatement = 'IF(%s, %s, %s)';
if (null === $databasePlatform) {
return sprintf($defaultStatement, $expression, $trueValue, $falseValue);
}

switch (get_class($databasePlatform)) {
case SqlitePlatform::class:
$statement = 'CASE WHEN %s THEN %s ELSE %s END';
break;
case 'postgresql':
case PostgreSQLPlatform::class:
return sprintf('COALESCE(%s, %s)', $falseValue, $trueValue);
default:
$statement = 'IF(%s, %s, %s)';
$statement = $defaultStatement;
}

return sprintf($statement, $expression, $trueValue, $falseValue);
Expand All @@ -301,7 +308,7 @@ public function getAllMenuNodes(
->where('n.deleted = false')
->addGroupBy('n.id');

if ($databasePlatformName === 'postgresql') {
if ($databasePlatform instanceof PostgreSQLPlatform) {
$qb->addGroupBy('t.url')
->addGroupby('t.id')
->addGroupby('v.weight')
Expand Down
9 changes: 8 additions & 1 deletion src/Kunstmaan/NodeBundle/Tests/Helper/UrlHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Kunstmaan\NodeBundle\Tests\Helper;

use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Schema\DefaultSchemaManagerFactory;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\ORM\EntityManager;
use Kunstmaan\AdminBundle\Helper\DomainConfigurationInterface;
Expand All @@ -20,7 +22,12 @@ class UrlHelperTest extends TestCase

protected function setUp(): void
{
$this->connection = DriverManager::getConnection(['driver' => 'pdo_sqlite', 'memory' => true]);
$configuration = new Configuration();
// NEXT_MAJOR: Remove check when dbal 2 support is removed.
if (method_exists($configuration, 'setSchemaManagerFactory')) {
$configuration->setSchemaManagerFactory(new DefaultSchemaManagerFactory());
}
$this->connection = DriverManager::getConnection(['driver' => 'pdo_sqlite', 'memory' => true], $configuration);
$this->createSchema();

$this->connection->transactional(
Expand Down
1 change: 1 addition & 0 deletions src/Kunstmaan/TranslatorBundle/Tests/app/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ doctrine:
default:
driver: pdo_sqlite
path: "%kernel.cache_dir%/test.db"
use_savepoints: true
orm:
default_entity_manager: default
entity_managers:
Expand Down

0 comments on commit fd5fb9c

Please sign in to comment.