Skip to content

Commit

Permalink
Merge pull request sonata-project#3337 from mihai-stancu/property_tra…
Browse files Browse the repository at this point in the history
…nsformer_array_support

Array support for property transformer when using $multiple = true
  • Loading branch information
OskarStark committed Oct 26, 2015
2 parents 2c11eb5 + 96db742 commit 3a37670
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 4 additions & 2 deletions Form/DataTransformer/ModelToIdPropertyTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,12 @@ public function transform($entityOrCollection)
if (!$entityOrCollection) {
return $result;
}

if ($this->multiple) {
if (substr(get_class($entityOrCollection), -1 * strlen($this->className)) == $this->className) {
$isArray = is_array($entityOrCollection);
if (!$isArray && substr(get_class($entityOrCollection), -1 * strlen($this->className)) == $this->className) {
throw new \InvalidArgumentException('A multiple selection must be passed a collection not a single value. Make sure that form option "multiple=false" is set for many-to-one relation and "multiple=true" is set for many-to-many or one-to-many relations.');
} elseif ($entityOrCollection instanceof \ArrayAccess) {
} elseif ($isArray || ($entityOrCollection instanceof \ArrayAccess)) {
$collection = $entityOrCollection;
} else {
throw new \InvalidArgumentException('A multiple selection must be passed a collection not a single value. Make sure that form option "multiple=false" is set for many-to-one relation and "multiple=true" is set for many-to-many or one-to-many relations.');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public function testTransformMultiple()
$collection[] = $entity2;
$collection[] = $entity3;

$this->modelManager->expects($this->exactly(3))
$this->modelManager->expects($this->exactly(6))
->method('getIdentifierValues')
->will($this->returnCallback(function ($value) use ($entity1, $entity2, $entity3) {
if ($value == $entity1) {
Expand All @@ -220,7 +220,9 @@ public function testTransformMultiple()
$this->assertSame(array('identifiers' => array(), 'labels' => array()), $transformer->transform(0));
$this->assertSame(array('identifiers' => array(), 'labels' => array()), $transformer->transform('0'));

$this->assertSame(array('identifiers' => array(123, 456, 789), 'labels' => array('foo', 'bar', 'baz')), $transformer->transform($collection));
$expected = array('identifiers' => array(123, 456, 789), 'labels' => array('foo', 'bar', 'baz'));
$this->assertSame($expected, $transformer->transform($collection));
$this->assertSame($expected, $transformer->transform($collection->toArray()));
}

/**
Expand Down

0 comments on commit 3a37670

Please sign in to comment.