|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Doctrine\ODM\MongoDB\Tests\Functional\Ticket; |
| 4 | + |
| 5 | +use Doctrine\ODM\MongoDB\DocumentRepository; |
| 6 | +use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; |
| 7 | +use Doctrine\ODM\MongoDB\PersistentCollection\PersistentCollectionInterface; |
| 8 | +use Doctrine\ODM\MongoDB\Tests\BaseTest; |
| 9 | + |
| 10 | +class GH1572Test extends BaseTest |
| 11 | +{ |
| 12 | + public function testPersistentCollectionCount() |
| 13 | + { |
| 14 | + $blog = new GH1572Blog(); |
| 15 | + $this->dm->persist($blog); |
| 16 | + |
| 17 | + $this->dm->persist(new GH1572Post($blog)); |
| 18 | + $this->dm->persist(new GH1572Post($blog)); |
| 19 | + $this->dm->persist(new GH1572Post($blog)); |
| 20 | + $this->dm->persist(new GH1572Post($blog)); |
| 21 | + |
| 22 | + $this->dm->flush(); |
| 23 | + $this->dm->refresh($blog); |
| 24 | + |
| 25 | + $this->assertInstanceOf(PersistentCollectionInterface::class, $blog->allPosts); |
| 26 | + $this->assertFalse($blog->allPosts->isInitialized()); |
| 27 | + $this->assertCount(4, $blog->allPosts); |
| 28 | + |
| 29 | + $this->assertInstanceOf(PersistentCollectionInterface::class, $blog->latestPosts); |
| 30 | + $this->assertFalse($blog->latestPosts->isInitialized()); |
| 31 | + $this->assertCount(2, $blog->latestPosts); |
| 32 | + |
| 33 | + $this->assertInstanceOf(PersistentCollectionInterface::class, $blog->latestPostsRepositoryMethod); |
| 34 | + $this->assertFalse($blog->latestPostsRepositoryMethod->isInitialized()); |
| 35 | + $this->assertCount(2, $blog->latestPostsRepositoryMethod); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +/** @ODM\Document */ |
| 40 | +class GH1572Blog |
| 41 | +{ |
| 42 | + /** @ODM\Id */ |
| 43 | + public $id; |
| 44 | + |
| 45 | + /** @ODM\ReferenceMany(targetDocument="GH1572Post", mappedBy="blog") */ |
| 46 | + public $allPosts = []; |
| 47 | + |
| 48 | + /** @ODM\ReferenceMany(targetDocument="GH1572Post", mappedBy="blog", sort={"id"="asc"}, limit=2) */ |
| 49 | + public $latestPosts = []; |
| 50 | + |
| 51 | + /** @ODM\ReferenceMany(targetDocument="GH1572Post", repositoryMethod="getPostsForBlog", limit=2) */ |
| 52 | + public $latestPostsRepositoryMethod = []; |
| 53 | +} |
| 54 | + |
| 55 | +/** @ODM\Document(repositoryClass="GH1572PostRepository") */ |
| 56 | +class GH1572Post |
| 57 | +{ |
| 58 | + /** @ODM\Id */ |
| 59 | + public $id; |
| 60 | + |
| 61 | + /** @ODM\ReferenceOne(targetDocument="GH1572Blog") */ |
| 62 | + public $blog; |
| 63 | + |
| 64 | + public function __construct(GH1572Blog $blog) |
| 65 | + { |
| 66 | + $this->blog = $blog; |
| 67 | + $blog->allPosts[] = $this; |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +class GH1572PostRepository extends DocumentRepository |
| 72 | +{ |
| 73 | + public function getPostsForBlog($blog) |
| 74 | + { |
| 75 | + return $this->createQueryBuilder() |
| 76 | + ->field('blog') |
| 77 | + ->references($blog) |
| 78 | + ->getQuery() |
| 79 | + ->getIterator(); |
| 80 | + } |
| 81 | +} |
0 commit comments