Skip to content

Commit 12790fa

Browse files
authored
#[MappedSuperclass] handles custom collection name (#2913)
`#[MappedSuperclass]` handles custom collection name Close #2910
2 parents 718826c + 1e00021 commit 12790fa

9 files changed

+188
-56
lines changed

.doctrine-project.json

Lines changed: 15 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,27 @@
66
"docsSlug": "doctrine-mongodb-odm",
77
"versions": [
88
{
9-
"name": "2.15",
10-
"branchName": "2.15.x",
9+
"name": "3.0",
10+
"branchName": "3.0.x",
11+
"slug": "3.0",
12+
"upcoming": true
13+
},
14+
{
15+
"name": "2.16",
16+
"branchName": "2.16.x",
1117
"slug": "latest",
1218
"upcoming": true
1319
},
20+
{
21+
"name": "2.15",
22+
"branchName": "2.15.x",
23+
"slug": "2.15",
24+
"current": true
25+
},
1426
{
1527
"name": "2.14",
16-
"branchName": "2.14.x",
1728
"slug": "2.14",
18-
"current": true
29+
"maintained": false
1930
},
2031
{
2132
"name": "2.13",
@@ -56,57 +67,6 @@
5667
"name": "2.6",
5768
"slug": "2.6",
5869
"maintained": false
59-
},
60-
{
61-
"name": "2.5",
62-
"slug": "2.5",
63-
"maintained": false
64-
},
65-
{
66-
"name": "2.4",
67-
"slug": "2.4",
68-
"maintained": false
69-
},
70-
{
71-
"name": "2.3",
72-
"slug": "2.3",
73-
"maintained": false
74-
},
75-
{
76-
"name": "2.2",
77-
"slug": "2.2",
78-
"maintained": false
79-
},
80-
{
81-
"name": "2.1",
82-
"slug": "2.1",
83-
"maintained": false
84-
},
85-
{
86-
"name": "2.0",
87-
"slug": "2.0",
88-
"maintained": false
89-
},
90-
{
91-
"name": "1.3",
92-
"branchName": "1.3.x",
93-
"slug": "1.3",
94-
"maintained": false
95-
},
96-
{
97-
"name": "1.2",
98-
"slug": "1.2",
99-
"maintained": false
100-
},
101-
{
102-
"name": "1.1",
103-
"slug": "1.1",
104-
"maintained": false
105-
},
106-
{
107-
"name": "1.0",
108-
"slug": "1.0",
109-
"maintained": false
11070
}
11171
]
11272
}

docs/en/reference/aggregation-builder.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ single result or return an iterator containing all results.
112112
If you instead want to look at the built aggregation pipeline, call the
113113
``Builder::getPipeline()`` method.
114114

115+
.. _aggregation_builder_hydration:
116+
115117
Hydration
116118
---------
117119

docs/en/reference/attributes-reference.rst

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,13 @@ The attribute is used to specify classes that are parents of document classes
737737
and should not be managed directly. See
738738
:ref:`inheritance mapping <inheritance_mapping>` for additional information.
739739

740+
Optional arguments:
741+
-
742+
``repositoryClass`` - Specifies a custom repository class to use.
743+
-
744+
``collection`` - By default, the collection name is derived from the
745+
class name. This option may be used to override that behavior.
746+
740747
.. code-block:: php
741748
742749
<?php
@@ -983,6 +990,45 @@ the method to be registered.
983990
984991
See :ref:`lifecycle_events` for more information.
985992

993+
#[QueryResultDocument]
994+
----------------------
995+
996+
Marks the document as query result document used when hydrating the aggregation query results.
997+
These documents can use all features regular documents can use but they will not be persisted to the database.
998+
999+
.. code-block:: php
1000+
1001+
<?php
1002+
1003+
#[QueryResultDocument]
1004+
class CompanyTransactions
1005+
{
1006+
#[ReferenceOne(targetDocument=Company::class, name="_id")]
1007+
private string $company;
1008+
1009+
#[Field]
1010+
private float $totalAmount;
1011+
}
1012+
1013+
Use the ``hydrate()`` method of the Aggregation Builder to specify the class
1014+
to use for hydration.
1015+
1016+
.. code-block:: php
1017+
1018+
<?php
1019+
1020+
$builder = $dm->createAggregationBuilder(Transaction::class);
1021+
$builder
1022+
->hydrate(CompanyTransactions::class)
1023+
->group()
1024+
->field('_id')
1025+
->expression('$company')
1026+
->field('totalAmount')
1027+
->sum('$amount');
1028+
1029+
1030+
See :ref:`aggregation_builder_hydration` for more information.
1031+
9861032
#[ReadPreference]
9871033
-----------------
9881034

src/Mapping/Annotations/MappedSuperclass.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@ final class MappedSuperclass extends AbstractDocument
2020
/** @var string|null */
2121
public $repositoryClass;
2222

23-
public function __construct(?string $repositoryClass = null)
23+
/** @var string|null */
24+
public $collection;
25+
26+
public function __construct(?string $repositoryClass = null, ?string $collection = null)
2427
{
2528
$this->repositoryClass = $repositoryClass;
29+
$this->collection = $collection;
2630
}
2731
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\ODM\MongoDB\Tests\Functional\Ticket;
6+
7+
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
8+
use Doctrine\ODM\MongoDB\Tests\BaseTestCase;
9+
10+
class GH2910Test extends BaseTestCase
11+
{
12+
public function testBirdCollectionNameIsAnimals(): void
13+
{
14+
$this->assertEquals('animals', $this->dm->getRepository(Bird2910::class)->getClassMetadata()->getCollection());
15+
}
16+
17+
public function testDogCollectionNameIsAnimals(): void
18+
{
19+
$this->assertEquals('animals', $this->dm->getRepository(Dog2910::class)->getClassMetadata()->getCollection());
20+
}
21+
}
22+
23+
#[ODM\MappedSuperclass(collection: 'animals')]
24+
#[ODM\InheritanceType('SINGLE_COLLECTION')]
25+
#[ODM\DiscriminatorField('classification')]
26+
#[ODM\DiscriminatorMap(['bird' => Bird2910::class, 'dog' => Dog2910::class])]
27+
class Animal2910
28+
{
29+
#[ODM\Id()]
30+
private string $id;
31+
}
32+
33+
#[ODM\Document]
34+
class Bird2910 extends Animal2910
35+
{
36+
}
37+
38+
#[ODM\Document]
39+
class Dog2910 extends Animal2910
40+
{
41+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<doctrine-mongo-mapping xmlns="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping
5+
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">
6+
7+
<mapped-superclass name="Doctrine\ODM\MongoDB\Tests\Functional\Ticket\Animal2910Xml"
8+
collection="animals"
9+
inheritance-type="SINGLE_COLLECTION">
10+
<discriminator-field name="classification" />
11+
<discriminator-map>
12+
<discriminator-mapping value="bird" class="Doctrine\ODM\MongoDB\Tests\Functional\Ticket\Bird2910Xml" />
13+
<discriminator-mapping value="dog" class="Doctrine\ODM\MongoDB\Tests\Functional\Ticket\Dog2910Xml" />
14+
</discriminator-map>
15+
16+
<id type="string" />
17+
</mapped-superclass>
18+
19+
</doctrine-mongo-mapping>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<doctrine-mongo-mapping xmlns="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping
5+
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">
6+
7+
<document name="Doctrine\ODM\MongoDB\Tests\Functional\Ticket\Bird2910Xml">
8+
</document>
9+
10+
</doctrine-mongo-mapping>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<doctrine-mongo-mapping xmlns="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping
5+
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">
6+
7+
<document name="Doctrine\ODM\MongoDB\Tests\Functional\Ticket\Dog2910Xml">
8+
</document>
9+
10+
</doctrine-mongo-mapping>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\ODM\MongoDB\Tests\Functional\Ticket;
6+
7+
use Doctrine\ODM\MongoDB\Mapping\Driver\XmlDriver;
8+
use Doctrine\ODM\MongoDB\Tests\BaseTestCase;
9+
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
10+
11+
class GH2910XmlTest extends BaseTestCase
12+
{
13+
public function testBirdCollectionNameIsAnimals(): void
14+
{
15+
$this->assertEquals('animals', $this->dm->getRepository(Bird2910Xml::class)->getClassMetadata()->getCollection());
16+
}
17+
18+
public function testDogCollectionNameIsAnimals(): void
19+
{
20+
$this->assertEquals('animals', $this->dm->getRepository(Dog2910Xml::class)->getClassMetadata()->getCollection());
21+
}
22+
23+
protected static function createMetadataDriverImpl(): MappingDriver
24+
{
25+
return new XmlDriver(__DIR__ . '/GH2910Xml');
26+
}
27+
}
28+
29+
class Animal2910Xml
30+
{
31+
private string $id;
32+
}
33+
34+
class Bird2910Xml extends Animal2910Xml
35+
{
36+
}
37+
38+
class Dog2910Xml extends Animal2910Xml
39+
{
40+
}

0 commit comments

Comments
 (0)