Skip to content
This repository
Browse code

Merge pull request #113 from bakura10/form/select-empty-values

Fix problem with empty tables
  • Loading branch information...
commit 349a7f95964636e749e202a0c7ccffab24e60a12 2 parents 820dc98 + f853a39
Marco Pivetta Ocramius authored
70 src/DoctrineModule/Form/Element/Proxy.php
@@ -188,44 +188,48 @@ protected function loadValueOptions()
188 188 $objects = $this->getObjects();
189 189 $options = array();
190 190
191   - foreach ($objects as $key => $object) {
192   - if (($property = $this->property)) {
193   - if (!$metadata->hasField($property)) {
194   - throw new RuntimeException(sprintf(
195   - 'Property "%s" could not be found in object "%s"',
196   - $property,
197   - $targetClass
198   - ));
199   - }
200   -
201   - $getter = 'get' . ucfirst($property);
202   - if (!is_callable(array($object, $getter))) {
203   - throw new RuntimeException(sprintf(
204   - 'Method "%s::%s" is not callable',
205   - $this->targetClass,
206   - $getter
207   - ));
  191 + if (empty($objects)) {
  192 + $options[''] = '';
  193 + } else {
  194 + foreach ($objects as $key => $object) {
  195 + if (($property = $this->property)) {
  196 + if (!$metadata->hasField($property)) {
  197 + throw new RuntimeException(sprintf(
  198 + 'Property "%s" could not be found in object "%s"',
  199 + $property,
  200 + $targetClass
  201 + ));
  202 + }
  203 +
  204 + $getter = 'get' . ucfirst($property);
  205 + if (!is_callable(array($object, $getter))) {
  206 + throw new RuntimeException(sprintf(
  207 + 'Method "%s::%s" is not callable',
  208 + $this->targetClass,
  209 + $getter
  210 + ));
  211 + }
  212 +
  213 + $label = $object->{$getter}();
  214 + } else {
  215 + if (!is_callable(array($object, '__toString'))) {
  216 + throw new RuntimeException(sprintf(
  217 + '%s must have a "__toString()" method defined if you have not set a property or method to use.',
  218 + $targetClass
  219 + ));
  220 + }
  221 +
  222 + $label = (string) $object;
208 223 }
209 224
210   - $label = $object->{$getter}();
211   - } else {
212   - if (!is_callable(array($object, '__toString'))) {
213   - throw new RuntimeException(sprintf(
214   - '%s must have a "__toString()" method defined if you have not set a property or method to use.',
215   - $targetClass
216   - ));
  225 + if (count($identifier) > 1) {
  226 + $value = $key;
  227 + } else {
  228 + $value = current($metadata->getIdentifierValues($object));
217 229 }
218 230
219   - $label = (string) $object;
  231 + $options[] = array('label' => $label, 'value' => $value);
220 232 }
221   -
222   - if (count($identifier) > 1) {
223   - $value = $key;
224   - } else {
225   - $value = current($metadata->getIdentifierValues($object));
226   - }
227   -
228   - $options[] = array('label' => $label, 'value' => $value);
229 233 }
230 234
231 235 $this->valueOptions = $options;
40 tests/DoctrineModuleTest/Form/Element/ProxyTest.php
@@ -117,6 +117,14 @@ public function testPublicPropertyUsedForGetValueOptions()
117 117 $this->assertEquals($result[1]['value'], 2);
118 118 }
119 119
  120 + public function testCanWorkWithEmptyTables()
  121 + {
  122 + $this->prepareEmptyProxy();
  123 +
  124 + $result = $this->proxy->getValueOptions();
  125 + $this->assertEquals(array(), $result);
  126 + }
  127 +
120 128 protected function prepareProxy()
121 129 {
122 130 $objectClass = 'DoctrineModuleTest\Form\Element\TestAsset\FormObject';
@@ -178,4 +186,36 @@ function() use ($objectOne, $objectTwo) {
178 186
179 187 $this->metadata = $metadata;
180 188 }
  189 +
  190 + public function prepareEmptyProxy()
  191 + {
  192 + $objectClass = 'DoctrineModuleTest\Form\Element\TestAsset\FormObject';
  193 +
  194 + $result = new ArrayCollection();
  195 +
  196 + $metadata = $this->getMock('Doctrine\Common\Persistence\Mapping\ClassMetadata');
  197 +
  198 + $objectRepository = $this->getMock('Doctrine\Common\Persistence\ObjectRepository');
  199 + $objectRepository->expects($this->once())
  200 + ->method('findAll')
  201 + ->will($this->returnValue($result));
  202 +
  203 + $objectManager = $this->getMock('Doctrine\Common\Persistence\ObjectManager');
  204 + $objectManager->expects($this->once())
  205 + ->method('getClassMetadata')
  206 + ->with($this->equalTo($objectClass))
  207 + ->will($this->returnValue($metadata));
  208 +
  209 + $objectManager->expects($this->once())
  210 + ->method('getRepository')
  211 + ->with($this->equalTo($objectClass))
  212 + ->will($this->returnValue($objectRepository));
  213 +
  214 + $this->proxy->setOptions(array(
  215 + 'object_manager' => $objectManager,
  216 + 'target_class' => $objectClass
  217 + ));
  218 +
  219 + $this->metadata = $metadata;
  220 + }
181 221 }

0 comments on commit 349a7f9

Please sign in to comment.
Something went wrong with that request. Please try again.