diff --git a/src/ZfrRest/Mvc/Controller/Plugin/ResourceModel.php b/src/ZfrRest/Mvc/Controller/Plugin/ResourceModel.php index 4f61beb..1fe2a1a 100644 --- a/src/ZfrRest/Mvc/Controller/Plugin/ResourceModel.php +++ b/src/ZfrRest/Mvc/Controller/Plugin/ResourceModel.php @@ -18,6 +18,9 @@ namespace ZfrRest\Mvc\Controller\Plugin; +use Doctrine\Common\Collections\Collection; +use Doctrine\Common\Collections\Criteria; +use Doctrine\Common\Collections\Selectable; use Zend\Mvc\Controller\Plugin\AbstractPlugin; use ZfrRest\Mvc\Controller\AbstractRestfulController; use ZfrRest\Mvc\Exception\RuntimeException; @@ -55,6 +58,12 @@ public function __invoke($data, ResourceMetadataInterface $resourceMetadata = nu $resourceMetadata = $resourceMetadata ?: $this->controller->getMatchedResource()->getMetadata(); + // When an URI like "/users" is matched, we may receive an ObjectRepository, that is Selectable + // BUT NOT iterable. Therefore we force a match with an empty Criteria for those cases + if ($data instanceof Selectable && !$data instanceof Collection) { + $data = $data->matching(new Criteria()); + } + return new ResourceViewModel(new Resource($data, $resourceMetadata)); } } diff --git a/src/ZfrRest/Router/Http/Matcher/CollectionSubPathMatcher.php b/src/ZfrRest/Router/Http/Matcher/CollectionSubPathMatcher.php index 08fae2f..1e6ffd4 100755 --- a/src/ZfrRest/Router/Http/Matcher/CollectionSubPathMatcher.php +++ b/src/ZfrRest/Router/Http/Matcher/CollectionSubPathMatcher.php @@ -88,7 +88,7 @@ protected function findItem(ResourceInterface $resource, $identifier) } $criteria = new Criteria(); - $criteria->andWhere($criteria->expr()->eq(reset($identifierNames), $identifier)); + $criteria->where($criteria->expr()->eq(reset($identifierNames), $identifier)); $found = $data->matching($criteria); diff --git a/tests/ZfrRestTest/Mvc/Controller/Plugin/ResourceModelTest.php b/tests/ZfrRestTest/Mvc/Controller/Plugin/ResourceModelTest.php index 814e5f2..41918c8 100644 --- a/tests/ZfrRestTest/Mvc/Controller/Plugin/ResourceModelTest.php +++ b/tests/ZfrRestTest/Mvc/Controller/Plugin/ResourceModelTest.php @@ -58,4 +58,25 @@ public function testCanCreateResourceModelWithMetadata() $this->assertSame($data, $resourceModel->getResource()->getData()); $this->assertSame($metadata, $resourceModel->getResource()->getMetadata()); } + + public function testForceSelectableToBeCollectionIfNotAlready() + { + $controller = new AbstractRestfulController(); + $plugin = new ResourceModel(); + $plugin->setController($controller); + + $metadata = $this->getMock('ZfrRest\Resource\Metadata\ResourceMetadataInterface'); + + $data = $this->getMock('Doctrine\Common\Collections\Selectable'); + $data->expects($this->once()) + ->method('matching') + ->with($this->isInstanceOf('Doctrine\Common\Collections\Criteria')) + ->will($this->returnValue($this->getMock('Doctrine\Common\Collections\Collection'))); + + $resourceModel = $plugin($data, $metadata); + + $this->assertInstanceOf('ZfrRest\View\Model\ResourceModel', $resourceModel); + //$this->assertInstanceOf('Doctrine\Common\Collections\Collection', $resourceModel->getResource()->getData()); + $this->assertSame($metadata, $resourceModel->getResource()->getMetadata()); + } }