Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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

Fix problem with empty tables
  • Loading branch information...
commit 349a7f95964636e749e202a0c7ccffab24e60a12 2 parents 820dc98 + f853a39
@Ocramius Ocramius authored
View
70 src/DoctrineModule/Form/Element/Proxy.php
@@ -188,44 +188,48 @@ protected function loadValueOptions()
$objects = $this->getObjects();
$options = array();
- foreach ($objects as $key => $object) {
- if (($property = $this->property)) {
- if (!$metadata->hasField($property)) {
- throw new RuntimeException(sprintf(
- 'Property "%s" could not be found in object "%s"',
- $property,
- $targetClass
- ));
- }
-
- $getter = 'get' . ucfirst($property);
- if (!is_callable(array($object, $getter))) {
- throw new RuntimeException(sprintf(
- 'Method "%s::%s" is not callable',
- $this->targetClass,
- $getter
- ));
+ if (empty($objects)) {
+ $options[''] = '';
+ } else {
+ foreach ($objects as $key => $object) {
+ if (($property = $this->property)) {
+ if (!$metadata->hasField($property)) {
+ throw new RuntimeException(sprintf(
+ 'Property "%s" could not be found in object "%s"',
+ $property,
+ $targetClass
+ ));
+ }
+
+ $getter = 'get' . ucfirst($property);
+ if (!is_callable(array($object, $getter))) {
+ throw new RuntimeException(sprintf(
+ 'Method "%s::%s" is not callable',
+ $this->targetClass,
+ $getter
+ ));
+ }
+
+ $label = $object->{$getter}();
+ } else {
+ if (!is_callable(array($object, '__toString'))) {
+ throw new RuntimeException(sprintf(
+ '%s must have a "__toString()" method defined if you have not set a property or method to use.',
+ $targetClass
+ ));
+ }
+
+ $label = (string) $object;
}
- $label = $object->{$getter}();
- } else {
- if (!is_callable(array($object, '__toString'))) {
- throw new RuntimeException(sprintf(
- '%s must have a "__toString()" method defined if you have not set a property or method to use.',
- $targetClass
- ));
+ if (count($identifier) > 1) {
+ $value = $key;
+ } else {
+ $value = current($metadata->getIdentifierValues($object));
}
- $label = (string) $object;
+ $options[] = array('label' => $label, 'value' => $value);
}
-
- if (count($identifier) > 1) {
- $value = $key;
- } else {
- $value = current($metadata->getIdentifierValues($object));
- }
-
- $options[] = array('label' => $label, 'value' => $value);
}
$this->valueOptions = $options;
View
40 tests/DoctrineModuleTest/Form/Element/ProxyTest.php
@@ -117,6 +117,14 @@ public function testPublicPropertyUsedForGetValueOptions()
$this->assertEquals($result[1]['value'], 2);
}
+ public function testCanWorkWithEmptyTables()
+ {
+ $this->prepareEmptyProxy();
+
+ $result = $this->proxy->getValueOptions();
+ $this->assertEquals(array(), $result);
+ }
+
protected function prepareProxy()
{
$objectClass = 'DoctrineModuleTest\Form\Element\TestAsset\FormObject';
@@ -178,4 +186,36 @@ function() use ($objectOne, $objectTwo) {
$this->metadata = $metadata;
}
+
+ public function prepareEmptyProxy()
+ {
+ $objectClass = 'DoctrineModuleTest\Form\Element\TestAsset\FormObject';
+
+ $result = new ArrayCollection();
+
+ $metadata = $this->getMock('Doctrine\Common\Persistence\Mapping\ClassMetadata');
+
+ $objectRepository = $this->getMock('Doctrine\Common\Persistence\ObjectRepository');
+ $objectRepository->expects($this->once())
+ ->method('findAll')
+ ->will($this->returnValue($result));
+
+ $objectManager = $this->getMock('Doctrine\Common\Persistence\ObjectManager');
+ $objectManager->expects($this->once())
+ ->method('getClassMetadata')
+ ->with($this->equalTo($objectClass))
+ ->will($this->returnValue($metadata));
+
+ $objectManager->expects($this->once())
+ ->method('getRepository')
+ ->with($this->equalTo($objectClass))
+ ->will($this->returnValue($objectRepository));
+
+ $this->proxy->setOptions(array(
+ 'object_manager' => $objectManager,
+ 'target_class' => $objectClass
+ ));
+
+ $this->metadata = $metadata;
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.