Skip to content

Commit

Permalink
Add support for Doctrine ORMv3/DBALv4 (KnpLabs#333)
Browse files Browse the repository at this point in the history
* add Doctrine 3 compatibility
* try to update the query analyzer
* Update dependencies because new major releases are available
* Keep compatibility of tests with doctrine/annotations
* Bump doctrine/orm version to make sure that the constructor of EntityManager is public
* Ignore annotation reader if not available
* Enable QueryAnalyzer in tests
* Do not try to alter "from" when counting results because resetQueryParts() is not available anymore in newer DBAL versions
* Test against doctrine/dbal 3.8

---------

Co-authored-by: Massimiliano Arione <[email protected]>
  • Loading branch information
W0rma and garak committed Apr 23, 2024
1 parent 9b1dcfb commit e689850
Show file tree
Hide file tree
Showing 19 changed files with 182 additions and 227 deletions.
12 changes: 7 additions & 5 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
name: PHPStan
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: PHPStan
uses: docker://oskarstark/phpstan-ga
env:
Expand All @@ -29,7 +29,7 @@ jobs:
name: PHP-CS-Fixer
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Fix CS
uses: docker://oskarstark/php-cs-fixer-ga
tests:
Expand All @@ -45,15 +45,17 @@ jobs:
php: '8.1'
- description: '8.2'
php: '8.2'
- description: '8.3'
php: '8.3'
- description: 'Dev deps'
php: '8.2'
php: '8.3'
dev: true
name: PHP ${{ matrix.php }} tests (${{ matrix.description }})
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Cache
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: ~/.composer/cache/files
key: ${{ matrix.php }}-${{ matrix.symfony }}-${{ matrix.composer_option }}
Expand Down
19 changes: 10 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,18 @@
},
"require-dev": {
"ext-pdo_sqlite": "*",
"doctrine/mongodb-odm": "^2.4",
"doctrine/orm": "^2.12",
"doctrine/phpcr-odm": "^1.6",
"jackalope/jackalope-doctrine-dbal": "^1.8",
"phpunit/phpunit": "^9.5",
"doctrine/dbal": "^3.8 || ^4.0",
"doctrine/mongodb-odm": "^2.5.5",
"doctrine/orm": "^2.13 || ^3.0",
"doctrine/phpcr-odm": "^1.8 || ^2.0",
"jackalope/jackalope-doctrine-dbal": "^1.12 || ^2.0",
"phpunit/phpunit": "^9.6",
"propel/propel1": "^1.7",
"ruflin/elastica": "^7.0",
"solarium/solarium": "^6.0",
"symfony/http-foundation": "^5.4 || ^6.0 || ^7.0",
"symfony/http-kernel": "^5.4 || ^6.0 || ^7.0",
"symfony/property-access": "^5.4 || ^6.0 || ^7.0"
"symfony/http-foundation": "^5.4.38 || ^6.4.4 || ^7.0",
"symfony/http-kernel": "^5.4.38 || ^6.4.4 || ^7.0",
"symfony/property-access": "^5.4.38 || ^6.4.4 || ^7.0"
},
"suggest": {
"doctrine/common": "to allow usage pagination with Doctrine ArrayCollection",
Expand All @@ -51,7 +52,7 @@
"symfony/property-access": "to allow sorting arrays"
},
"conflict": {
"doctrine/dbal": "<3.1"
"doctrine/dbal": "<3.8"
},
"extra": {
"branch-alias": {
Expand Down
6 changes: 5 additions & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
parameters:
ignoreErrors:
-
message: "#^Call to an undefined method Doctrine\\\\DBAL\\\\Query\\\\QueryBuilder\\:\\:resetQueryParts\\(\\)\\.$#"
count: 1
path: src/Knp/Component/Pager/Event/Subscriber/Paginate/Doctrine/DBALQueryBuilderSubscriber.php

-
message: "#^Method Knp\\\\Component\\\\Pager\\\\Event\\\\Subscriber\\\\Sortable\\\\ArraySubscriber\\:\\:sortFunction\\(\\) has parameter \\$object1 with no value type specified in iterable type array\\.$#"
count: 1
Expand Down Expand Up @@ -29,4 +34,3 @@ parameters:
message: "#^Interface Knp\\\\Component\\\\Pager\\\\Pagination\\\\PaginationInterface extends generic interface ArrayAccess but does not specify its types\\: TKey, TValue$#"
count: 1
path: src/Knp/Component/Pager/Pagination/PaginationInterface.php

Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ public function items(ItemsEvent $event): void
$qb = clone $target;

//reset count orderBy since it can break query and slow it down
$qb
->resetQueryPart('orderBy')
;

if (method_exists($qb, 'resetOrderBy')) {
$qb->resetOrderBy();
} else {
$qb->resetQueryParts(['orderBy']);
}

// get the query
$sql = $qb->getSQL();

$qb
->resetQueryParts()
->select('count(*) as cnt')
->from('(' . $sql . ')', 'dbal_count_tbl')
;

$compat = $qb->executeQuery();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function items(ItemsEvent $event): void
}

$queryCount = clone $event->target;
$event->count = $queryCount->execute(null, Query::HYDRATE_PHPCR)->getRows()->count();
$event->count = iterator_count($queryCount->execute(null, Query::HYDRATE_PHPCR)->getRows());

$query = $event->target;
$query->setMaxResults($event->getLimit());
Expand Down
5 changes: 5 additions & 0 deletions tests/Test/Fixture/Document/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,31 @@
/**
* @ODM\Document
*/
#[ODM\Document]
final class Article
{
/**
* @ODM\Id
*/
#[ODM\Id]
private $id;

/**
* @ODM\Field(type="string")
*/
#[ODM\Field(type: "string")]
private ?string $title = null;

/**
* @ODM\Field(type="bool", name="status")
*/
#[ODM\Field(type: "bool")]
private bool $status = false;

/**
* @ODM\Field(type="date", name="created_at")
*/
#[ODM\Field(type: "date", name: "created_at")]
private ?\DateTime $createdAt = null;

public function getId()
Expand Down
5 changes: 5 additions & 0 deletions tests/Test/Fixture/Document/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,32 @@

namespace Test\Fixture\Document;

use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCRAnnotations;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

/**
* @ODM\Document
*/
#[ODM\Document]
final class Image
{
/**
* @ODM\Id
*/
#[ODM\Id]
private $id;

/**
* @ODM\Field
*/
#[ODM\Field]
private ?string $title = null;

/**
* @ODM\File
* @var int|string
*/
#[ODM\File]
private $file;

/**
Expand Down
15 changes: 10 additions & 5 deletions tests/Test/Fixture/Document/PHPCR/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,31 @@

namespace Test\Fixture\Document\PHPCR;

use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCR;
use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCRAnnotations;
use Doctrine\ODM\PHPCR\Mapping\Attributes as PHPCR;

/**
* @PHPCR\Document
* @PHPCRAnnotations\Document
*/
#[PHPCR\Document]
final class Article
{
/**
* @PHPCR\Id
* @PHPCRAnnotations\Id
*/
#[PHPCR\Id]
private $id;

/**
* @PHPCR\ParentDocument
* @PHPCRAnnotations\ParentDocument
*/
#[PHPCR\ParentDocument]
private $parent;

/**
* @PHPCR\Field(type="string")
* @PHPCRAnnotations\Field(type="string")
*/
#[PHPCR\Field(type: "string")]
private $title;

public function getId()
Expand Down
4 changes: 4 additions & 0 deletions tests/Test/Fixture/Entity/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,27 @@
/**
* @ORM\Entity
*/
#[ORM\Entity]
class Article
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
#[ORM\Id, ORM\GeneratedValue, ORM\Column(type: "integer")]
private $id;

/**
* @ORM\Column(length=64)
*/
#[ORM\Column(length: 64)]
private ?string $title = null;

/**
* @ORM\Column(type="boolean")
*/
#[ORM\Column(type: "boolean")]
private bool $enabled = true;

public function getId()
Expand Down
4 changes: 4 additions & 0 deletions tests/Test/Fixture/Entity/Composite.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,27 @@
/**
* @ORM\Entity
*/
#[ORM\Entity]
class Composite
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
*/
#[ORM\Id, ORM\Column(type: "integer")]
private $id;

/**
* @ORM\Column(length=64)
*/
#[ORM\Column(length: 64)]
private ?string $title = null;

/**
* @ORM\Id
* @ORM\Column(type="string")
*/
#[ORM\Id, ORM\Column(type: "string")]
private ?string $uid = null;

public function setUid(?string $uid): void
Expand Down
7 changes: 7 additions & 0 deletions tests/Test/Fixture/Entity/Shop/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,45 @@
/**
* @ORM\Entity
*/
#[ORM\Entity]
class Product
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
#[ORM\Id, ORM\GeneratedValue, ORM\Column(type: "integer")]
private $id;

/**
* @ORM\Column(length=64)
*/
#[ORM\Column(length: 64)]
private ?string $title = null;

/**
* @ORM\Column(length=255, nullable=true)
*/
#[ORM\Column(length: 255, nullable: true)]
private ?string $description = null;

/**
* @ORM\Column(type="float", nullable=false)
*/
#[ORM\Column(type: "float", nullable: true)]
private ?float $price = null;

/**
* @ORM\ManyToMany(targetEntity="Tag")
*/
#[ORM\ManyToMany(targetEntity: "Tag")]
private Collection $tags;

/**
* @ORM\Column(type="integer")
*/
#[ORM\Column(type: "integer")]
private int $numTags = 0;

public function __construct()
Expand Down
3 changes: 3 additions & 0 deletions tests/Test/Fixture/Entity/Shop/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@
/**
* @ORM\Entity
*/
#[ORM\Entity]
class Tag
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
#[ORM\Id, ORM\GeneratedValue, ORM\Column(type: "integer")]
private $id;

/**
* @ORM\Column(length=64)
*/
#[ORM\Column(length: 64)]
private ?string $name = null;

public function getId()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Test\Pager\Subscriber\Filtration\Doctrine\ORM;

use Doctrine\DBAL\DriverManager;
use Doctrine\ORM\EntityManagerInterface;
use Knp\Component\Pager\ArgumentAccess\RequestArgumentAccess;
use Knp\Component\Pager\Event\Subscriber\Filtration\Doctrine\ORM\Query\WhereWalker;
Expand Down Expand Up @@ -39,8 +40,9 @@ public function shouldHandleApcQueryCache(): void
'memory' => true,
];

$em = \Doctrine\ORM\EntityManager::create($conn, $config);
$schema = \array_map(static function ($class) use ($em) {
$connection = DriverManager::getConnection($conn, $config);
$em = new \Doctrine\ORM\EntityManager($connection, $config);
$schema = \array_map(static function (string $class) use ($em) {
return $em->getClassMetadata($class);
}, $this->getUsedEntityFixtures());

Expand Down Expand Up @@ -756,7 +758,7 @@ public function shouldFilterCaseInsensitiveWhenAsked(): void
}

/**
* @return Article[]
* @return string[]
*/
protected function getUsedEntityFixtures(): array
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Test\Pager\Subscriber\Sortable\Doctrine\ORM;

use Doctrine\DBAL\DriverManager;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\SchemaTool;
use Knp\Component\Pager\ArgumentAccess\RequestArgumentAccess;
Expand Down Expand Up @@ -38,7 +39,8 @@ public function shouldHandleApcQueryCache(): void
'memory' => true,
];

$em = EntityManager::create($conn, $config);
$connection = DriverManager::getConnection($conn, $config);
$em = new EntityManager($connection, $config);
$schema = \array_map(static function ($class) use ($em) {
return $em->getClassMetadata($class);
}, $this->getUsedEntityFixtures());
Expand Down
Loading

0 comments on commit e689850

Please sign in to comment.