Skip to content

Commit e689850

Browse files
W0rmagarak
andauthored
Add support for Doctrine ORMv3/DBALv4 (#333)
* 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 <garakkio@gmail.com>
1 parent 9b1dcfb commit e689850

File tree

19 files changed

+182
-227
lines changed

19 files changed

+182
-227
lines changed

.github/workflows/build.yaml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
name: PHPStan
1717
steps:
1818
- name: Checkout
19-
uses: actions/checkout@v3
19+
uses: actions/checkout@v4
2020
- name: PHPStan
2121
uses: docker://oskarstark/phpstan-ga
2222
env:
@@ -29,7 +29,7 @@ jobs:
2929
name: PHP-CS-Fixer
3030
steps:
3131
- name: Checkout
32-
uses: actions/checkout@v3
32+
uses: actions/checkout@v4
3333
- name: Fix CS
3434
uses: docker://oskarstark/php-cs-fixer-ga
3535
tests:
@@ -45,15 +45,17 @@ jobs:
4545
php: '8.1'
4646
- description: '8.2'
4747
php: '8.2'
48+
- description: '8.3'
49+
php: '8.3'
4850
- description: 'Dev deps'
49-
php: '8.2'
51+
php: '8.3'
5052
dev: true
5153
name: PHP ${{ matrix.php }} tests (${{ matrix.description }})
5254
steps:
5355
- name: Checkout
54-
uses: actions/checkout@v3
56+
uses: actions/checkout@v4
5557
- name: Cache
56-
uses: actions/cache@v3
58+
uses: actions/cache@v4
5759
with:
5860
path: ~/.composer/cache/files
5961
key: ${{ matrix.php }}-${{ matrix.symfony }}-${{ matrix.composer_option }}

composer.json

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,18 @@
2727
},
2828
"require-dev": {
2929
"ext-pdo_sqlite": "*",
30-
"doctrine/mongodb-odm": "^2.4",
31-
"doctrine/orm": "^2.12",
32-
"doctrine/phpcr-odm": "^1.6",
33-
"jackalope/jackalope-doctrine-dbal": "^1.8",
34-
"phpunit/phpunit": "^9.5",
30+
"doctrine/dbal": "^3.8 || ^4.0",
31+
"doctrine/mongodb-odm": "^2.5.5",
32+
"doctrine/orm": "^2.13 || ^3.0",
33+
"doctrine/phpcr-odm": "^1.8 || ^2.0",
34+
"jackalope/jackalope-doctrine-dbal": "^1.12 || ^2.0",
35+
"phpunit/phpunit": "^9.6",
3536
"propel/propel1": "^1.7",
3637
"ruflin/elastica": "^7.0",
3738
"solarium/solarium": "^6.0",
38-
"symfony/http-foundation": "^5.4 || ^6.0 || ^7.0",
39-
"symfony/http-kernel": "^5.4 || ^6.0 || ^7.0",
40-
"symfony/property-access": "^5.4 || ^6.0 || ^7.0"
39+
"symfony/http-foundation": "^5.4.38 || ^6.4.4 || ^7.0",
40+
"symfony/http-kernel": "^5.4.38 || ^6.4.4 || ^7.0",
41+
"symfony/property-access": "^5.4.38 || ^6.4.4 || ^7.0"
4142
},
4243
"suggest": {
4344
"doctrine/common": "to allow usage pagination with Doctrine ArrayCollection",
@@ -51,7 +52,7 @@
5152
"symfony/property-access": "to allow sorting arrays"
5253
},
5354
"conflict": {
54-
"doctrine/dbal": "<3.1"
55+
"doctrine/dbal": "<3.8"
5556
},
5657
"extra": {
5758
"branch-alias": {

phpstan-baseline.neon

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
parameters:
22
ignoreErrors:
3+
-
4+
message: "#^Call to an undefined method Doctrine\\\\DBAL\\\\Query\\\\QueryBuilder\\:\\:resetQueryParts\\(\\)\\.$#"
5+
count: 1
6+
path: src/Knp/Component/Pager/Event/Subscriber/Paginate/Doctrine/DBALQueryBuilderSubscriber.php
7+
38
-
49
message: "#^Method Knp\\\\Component\\\\Pager\\\\Event\\\\Subscriber\\\\Sortable\\\\ArraySubscriber\\:\\:sortFunction\\(\\) has parameter \\$object1 with no value type specified in iterable type array\\.$#"
510
count: 1
@@ -29,4 +34,3 @@ parameters:
2934
message: "#^Interface Knp\\\\Component\\\\Pager\\\\Pagination\\\\PaginationInterface extends generic interface ArrayAccess but does not specify its types\\: TKey, TValue$#"
3035
count: 1
3136
path: src/Knp/Component/Pager/Pagination/PaginationInterface.php
32-

src/Knp/Component/Pager/Event/Subscriber/Paginate/Doctrine/DBALQueryBuilderSubscriber.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@ public function items(ItemsEvent $event): void
2222
$qb = clone $target;
2323

2424
//reset count orderBy since it can break query and slow it down
25-
$qb
26-
->resetQueryPart('orderBy')
27-
;
28-
25+
if (method_exists($qb, 'resetOrderBy')) {
26+
$qb->resetOrderBy();
27+
} else {
28+
$qb->resetQueryParts(['orderBy']);
29+
}
30+
2931
// get the query
3032
$sql = $qb->getSQL();
31-
33+
3234
$qb
33-
->resetQueryParts()
3435
->select('count(*) as cnt')
35-
->from('(' . $sql . ')', 'dbal_count_tbl')
3636
;
3737

3838
$compat = $qb->executeQuery();

src/Knp/Component/Pager/Event/Subscriber/Paginate/Doctrine/ODM/PHPCR/QuerySubscriber.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function items(ItemsEvent $event): void
1818
}
1919

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

2323
$query = $event->target;
2424
$query->setMaxResults($event->getLimit());

tests/Test/Fixture/Document/Article.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,31 @@
77
/**
88
* @ODM\Document
99
*/
10+
#[ODM\Document]
1011
final class Article
1112
{
1213
/**
1314
* @ODM\Id
1415
*/
16+
#[ODM\Id]
1517
private $id;
1618

1719
/**
1820
* @ODM\Field(type="string")
1921
*/
22+
#[ODM\Field(type: "string")]
2023
private ?string $title = null;
2124

2225
/**
2326
* @ODM\Field(type="bool", name="status")
2427
*/
28+
#[ODM\Field(type: "bool")]
2529
private bool $status = false;
2630

2731
/**
2832
* @ODM\Field(type="date", name="created_at")
2933
*/
34+
#[ODM\Field(type: "date", name: "created_at")]
3035
private ?\DateTime $createdAt = null;
3136

3237
public function getId()

tests/Test/Fixture/Document/Image.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,32 @@
22

33
namespace Test\Fixture\Document;
44

5+
use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCRAnnotations;
56
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
67

78
/**
89
* @ODM\Document
910
*/
11+
#[ODM\Document]
1012
final class Image
1113
{
1214
/**
1315
* @ODM\Id
1416
*/
17+
#[ODM\Id]
1518
private $id;
1619

1720
/**
1821
* @ODM\Field
1922
*/
23+
#[ODM\Field]
2024
private ?string $title = null;
2125

2226
/**
2327
* @ODM\File
2428
* @var int|string
2529
*/
30+
#[ODM\File]
2631
private $file;
2732

2833
/**

tests/Test/Fixture/Document/PHPCR/Article.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,31 @@
22

33
namespace Test\Fixture\Document\PHPCR;
44

5-
use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCR;
5+
use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCRAnnotations;
6+
use Doctrine\ODM\PHPCR\Mapping\Attributes as PHPCR;
67

78
/**
8-
* @PHPCR\Document
9+
* @PHPCRAnnotations\Document
910
*/
11+
#[PHPCR\Document]
1012
final class Article
1113
{
1214
/**
13-
* @PHPCR\Id
15+
* @PHPCRAnnotations\Id
1416
*/
17+
#[PHPCR\Id]
1518
private $id;
1619

1720
/**
18-
* @PHPCR\ParentDocument
21+
* @PHPCRAnnotations\ParentDocument
1922
*/
23+
#[PHPCR\ParentDocument]
2024
private $parent;
2125

2226
/**
23-
* @PHPCR\Field(type="string")
27+
* @PHPCRAnnotations\Field(type="string")
2428
*/
29+
#[PHPCR\Field(type: "string")]
2530
private $title;
2631

2732
public function getId()

tests/Test/Fixture/Entity/Article.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,27 @@
77
/**
88
* @ORM\Entity
99
*/
10+
#[ORM\Entity]
1011
class Article
1112
{
1213
/**
1314
* @ORM\Id
1415
* @ORM\GeneratedValue
1516
* @ORM\Column(type="integer")
1617
*/
18+
#[ORM\Id, ORM\GeneratedValue, ORM\Column(type: "integer")]
1719
private $id;
1820

1921
/**
2022
* @ORM\Column(length=64)
2123
*/
24+
#[ORM\Column(length: 64)]
2225
private ?string $title = null;
2326

2427
/**
2528
* @ORM\Column(type="boolean")
2629
*/
30+
#[ORM\Column(type: "boolean")]
2731
private bool $enabled = true;
2832

2933
public function getId()

tests/Test/Fixture/Entity/Composite.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,27 @@
77
/**
88
* @ORM\Entity
99
*/
10+
#[ORM\Entity]
1011
class Composite
1112
{
1213
/**
1314
* @ORM\Id
1415
* @ORM\Column(type="integer")
1516
*/
17+
#[ORM\Id, ORM\Column(type: "integer")]
1618
private $id;
1719

1820
/**
1921
* @ORM\Column(length=64)
2022
*/
23+
#[ORM\Column(length: 64)]
2124
private ?string $title = null;
2225

2326
/**
2427
* @ORM\Id
2528
* @ORM\Column(type="string")
2629
*/
30+
#[ORM\Id, ORM\Column(type: "string")]
2731
private ?string $uid = null;
2832

2933
public function setUid(?string $uid): void

0 commit comments

Comments
 (0)