From 4db25e5cfec503de4e9a89f05d76746a9b94e4d6 Mon Sep 17 00:00:00 2001 From: Volodymyr Telnov Date: Thu, 24 May 2018 18:46:53 +0300 Subject: [PATCH 1/4] Some errors or confusing class names changed --- .../mapping-classes-to-orm-and-odm.rst | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) 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 fb8f294191..68867f477e 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,8 +17,8 @@ for multiple Doctrine persistence layers: - + @@ -79,8 +79,8 @@ First define the mapping for the ORM: .. code-block:: yaml - Documents\BlogPost: - repositoryClass: Doctrine\Blog\ORM\BlogPostRepository + Documents\Blog\BlogPost: + repositoryClass: Documents\Blog\ORM\BlogPostRepository id: id: type: integer @@ -90,7 +90,7 @@ First define the mapping for the ORM: body: type: text -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 @@ -108,7 +108,7 @@ You can find the blog post: getRepository('Documents\BlogPost')->findOneByTitle('test'); + $blogPost = $em->getRepository('Documents\Blog\BlogPost')->findOneBy(array('title' => 'test')); MongoDB ODM ~~~~~~~~~~~ @@ -121,9 +121,9 @@ Now map the same class to the Doctrine MongoDB ODM: - + @@ -156,7 +156,7 @@ Now map the same class to the Doctrine MongoDB ODM: .. code-block:: yaml Documents\BlogPost: - repositoryClass: Doctrine\Blog\ODM\MongoDB\BlogPostRepository + repositoryClass: Documents\Blog\ODM\MongoDB\BlogPostRepository fields: id: type: id @@ -183,7 +183,7 @@ You can find the blog post: getRepository('Documents\BlogPost')->findOneByTitle('test'); + $blogPost = $dm->getRepository('Documents\Blog\BlogPost')->findOneBy(array('title' => 'test')); Repository Classes ------------------ @@ -194,7 +194,7 @@ You can implement the same repository interface for the ORM and MongoDB ODM easi Date: Sun, 27 May 2018 11:41:48 +0300 Subject: [PATCH 2/4] Updated RepositoryClass::class syntax and added an Interface In ``getRepository()`` method changed string literal to ``RepositoryClass::class`` name constant. Also added a couple of missing semicolons. And to enforce common interfaces for repositories, introduced a stub for ``BlogPostRepositoryInterface`` interface to be implemented by ORM and ODM Repository classes. --- .../mapping-classes-to-orm-and-odm.rst | 32 +++++++++++++++---- 1 file changed, 25 insertions(+), 7 deletions(-) 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 68867f477e..d9507c2f75 100644 --- a/docs/en/cookbook/mapping-classes-to-orm-and-odm.rst +++ b/docs/en/cookbook/mapping-classes-to-orm-and-odm.rst @@ -96,7 +96,7 @@ Now you are able to persist the ``Documents\Blog\BlogPost`` with an instance of setTitle('test'); $em->persist($blogPost); @@ -108,7 +108,7 @@ You can find the blog post: getRepository('Documents\Blog\BlogPost')->findOneBy(array('title' => 'test')); + $blogPost = $em->getRepository(BlogPost::class)->findOneBy(array('title' => 'test')); MongoDB ODM ~~~~~~~~~~~ @@ -171,7 +171,7 @@ Now the same class is able to be persisted in the same way using an instance of setTitle('test'); $dm->persist($blogPost); @@ -183,12 +183,28 @@ You can find the blog post: getRepository('Documents\Blog\BlogPost')->findOneBy(array('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 + + Date: Mon, 29 Apr 2019 22:55:33 +0200 Subject: [PATCH 3/4] Adapt API to BlogPostRepositoryInterface --- docs/en/cookbook/mapping-classes-to-orm-and-odm.rst | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) 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 d9507c2f75..e47655d07e 100644 --- a/docs/en/cookbook/mapping-classes-to-orm-and-odm.rst +++ b/docs/en/cookbook/mapping-classes-to-orm-and-odm.rst @@ -47,7 +47,9 @@ First define the mapping for the ORM: namespace Documents\Blog; - /** @Entity(repositoryClass="Documents\Blog\Repository\ORM\BlogPostRepository") */ + use Documents\Blog\Repository\ORM\BlogPostRepository; + + /** @Entity(repositoryClass=BlogPostRepository::class) */ class BlogPost { /** @Id @Column(type="integer") */ @@ -123,7 +125,9 @@ Now map the same class to the Doctrine MongoDB ODM: namespace Documents\Blog; - /** @Document(repositoryClass="Documents\Blog\Repository\ODM\BlogPostRepository") */ + use Documents\Blog\Repository\ODM\BlogPostRepository; + + /** @Document(repositoryClass=BlogPostRepository::class) */ class BlogPost { /** @Id */ @@ -147,7 +151,7 @@ Now map the same class to the Doctrine MongoDB ODM: http://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd"> - + From 72c5366e0eea6f728861b51992060a5ef1af9a08 Mon Sep 17 00:00:00 2001 From: aturki Date: Mon, 29 Apr 2019 09:40:19 +0200 Subject: [PATCH 4/4] Don't apply filters and discriminators when using $indexStats --- lib/Doctrine/ODM/MongoDB/Aggregation/Builder.php | 7 +++++++ .../ODM/MongoDB/Tests/Aggregation/BuilderTest.php | 12 ++++++++++++ 2 files changed, 19 insertions(+) diff --git a/lib/Doctrine/ODM/MongoDB/Aggregation/Builder.php b/lib/Doctrine/ODM/MongoDB/Aggregation/Builder.php index deafcbc869..c6bfe9d820 100644 --- a/lib/Doctrine/ODM/MongoDB/Aggregation/Builder.php +++ b/lib/Doctrine/ODM/MongoDB/Aggregation/Builder.php @@ -21,6 +21,7 @@ use Doctrine\MongoDB\Aggregation\Builder as BaseBuilder; use Doctrine\MongoDB\Aggregation\Stage\GeoNear; +use Doctrine\MongoDB\Aggregation\Stage\IndexStats; use Doctrine\MongoDB\CommandCursor as BaseCommandCursor; use Doctrine\ODM\MongoDB\CommandCursor; use Doctrine\ODM\MongoDB\DocumentManager; @@ -214,6 +215,12 @@ public function getPipeline() if ($this->getStage(0) instanceof GeoNear) { $pipeline[0]['$geoNear']['query'] = $this->applyFilters($pipeline[0]['$geoNear']['query']); + } elseif ($this->getStage(0) instanceof 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 { $matchStage = $this->applyFilters([]); if ($matchStage !== []) { diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Aggregation/BuilderTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Aggregation/BuilderTest.php index c2f1585001..0742803c2e 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Aggregation/BuilderTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Aggregation/BuilderTest.php @@ -2,6 +2,9 @@ namespace Doctrine\ODM\MongoDB\Tests\Aggregation; +use Documents\Project; +use stdClass; + class BuilderTest extends \Doctrine\ODM\MongoDB\Tests\BaseTest { public function testAggregationBuilder() @@ -183,6 +186,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 \Documents\Tag('baseball');