diff --git a/docs/en/cookbook/mapping-classes-to-orm-and-odm.rst b/docs/en/cookbook/mapping-classes-to-orm-and-odm.rst index 1ed7268b58..d96ec1f6e2 100644 --- a/docs/en/cookbook/mapping-classes-to-orm-and-odm.rst +++ b/docs/en/cookbook/mapping-classes-to-orm-and-odm.rst @@ -1,15 +1,15 @@ Mapping Classes to the ORM and ODM ================================== -Because of the non intrusive design of Doctrine it is possible for you to have plain PHP classes -that are mapped to both a relational database with the Doctrine2 Object Relational Mapper and -MongoDB with the Doctrine MongoDB Object Document Mapper, or any other persistence layer that +Because of the non-intrusive design of Doctrine, it is possible for you to have plain PHP classes +that are mapped to both a relational database (with the Doctrine2 Object Relational Mapper) and +MongoDB (with the Doctrine MongoDB Object Document Mapper), or any other persistence layer that implements the Doctrine Common `persistence`_ interfaces. Test Subject ------------ -For this cookbook entry we need to define a class that can be persisted to both MySQL and MongoDB. +For this cookbook entry, we need to define a class that can be persisted to both MySQL and MongoDB. We'll use a ``BlogPost`` as you may want to write some generic blogging functionality that has support for multiple Doctrine persistence layers: @@ -17,7 +17,7 @@ for multiple Doctrine persistence layers: - + -Now you are able to persist the ``Documents\BlogPost`` with an instance of ``EntityManager``: +Now you are able to persist the ``Documents\Blog\BlogPost`` with an instance of ``EntityManager``: .. code-block:: php setTitle('test'); $em->persist($blogPost); @@ -95,7 +97,7 @@ You can find the blog post: getRepository(Documents\BlogPost::class)->findOneBy(['title' => 'test']); + $blogPost = $em->getRepository(BlogPost::class)->findOneBy(array('title' => 'test')); MongoDB ODM ~~~~~~~~~~~ @@ -108,9 +110,11 @@ Now map the same class to the Doctrine MongoDB ODM: - - + + @@ -146,7 +150,7 @@ Now the same class is able to be persisted in the same way using an instance of setTitle('test'); $dm->persist($blogPost); @@ -158,23 +162,39 @@ You can find the blog post: getRepository(Documents\BlogPost::class)->findOneBy(['title' => 'test']); + $blogPost = $dm->getRepository(BlogPost::class)->findOneBy(array('title' => 'test')); Repository Classes ------------------ -You can implement the same repository interface for the ORM and MongoDB ODM easily: +You can implement the same repository interface for the ORM and MongoDB ODM easily, e.g. by creating ``BlogPostRepositoryInterface``: .. code-block:: php findOneBy(['id' => $id]); } diff --git a/lib/Doctrine/ODM/MongoDB/Aggregation/Builder.php b/lib/Doctrine/ODM/MongoDB/Aggregation/Builder.php index dfd17488eb..c2ff35196e 100644 --- a/lib/Doctrine/ODM/MongoDB/Aggregation/Builder.php +++ b/lib/Doctrine/ODM/MongoDB/Aggregation/Builder.php @@ -233,6 +233,12 @@ static function (Stage $stage) { if ($this->getStage(0) instanceof Stage\GeoNear) { $pipeline[0]['$geoNear']['query'] = $this->applyFilters($pipeline[0]['$geoNear']['query']); + } elseif ($this->getStage(0) instanceof Stage\IndexStats) { + // Don't apply any filters when using an IndexStats stage: since it + // needs to be the first pipeline stage, prepending a match stage + // with discriminator information will not work + + return $pipeline; } else { $matchExpression = $this->applyFilters([]); if ($matchExpression !== []) { diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Aggregation/BuilderTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Aggregation/BuilderTest.php index ae39926ee6..a131f1f66d 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Aggregation/BuilderTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Aggregation/BuilderTest.php @@ -13,8 +13,10 @@ use Documents\BlogTagAggregation; use Documents\CmsComment; use Documents\GuestServer; +use Documents\Project; use Documents\Tag; use MongoDB\BSON\UTCDateTime; +use function array_keys; class BuilderTest extends BaseTest { @@ -343,6 +345,15 @@ public function testBuilderWithOutStageReturnsNoData() $this->assertCount(0, $result); } + public function testBuilderWithIndexStatsStageDoesNotApplyFilters() + { + $builder = $this->dm + ->createAggregationBuilder(Project::class) + ->indexStats(); + + $this->assertSame('$indexStats', array_keys($builder->getPipeline()[0])[0]); + } + private function insertTestData() { $baseballTag = new Tag('baseball');