Skip to content

Commit

Permalink
Merge pull request #13 from chernecov/collection-transformation-support
Browse files Browse the repository at this point in the history
[CrudTranformer] Added ability to transform associated collections.
  • Loading branch information
chernecov committed Aug 26, 2015
2 parents 786a740 + dfaaa32 commit 0389eda
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion Services/CRUD/CrudTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,10 @@ public function transformPropertyValue($property, $value, ArrayCollection $colle
if ($this->transformationNeeded($property, $value)) {
$targetClass = $this->getClassMetadata()->getAssociationTargetClass(ucfirst($property));

if ($value instanceof $targetClass) {
return $value;
}

if (is_null($collection)) {
$value = $this->processValue($value, $targetClass);
} else {
Expand Down Expand Up @@ -342,7 +346,21 @@ public function processPropertyValue($object, $property, $value, $action, ArrayC
if (!$this->isPropertyAccessible($property, $action)) {
return;
}
$value = $this->transformPropertyValue($property, $value, $collection);

$property = Inflector::camelize($property);
if ($this->getClassMetadata()->hasAssociation(ucfirst($property))) {
$property = ucfirst($property);
}
if ($this->getClassMetadata()->isCollectionValuedAssociation($property)) {
$result = new ArrayCollection();
foreach ($value as $valueItem) {
$result->add($this->transformPropertyValue($property, $valueItem, $collection));
}
$value = $result;
} else {
$value = $this->transformPropertyValue($property, $value, $collection);
}

$method = $this->getPropertySetter($property);
if (method_exists($object, $method)) {
$object->$method($value);
Expand Down

0 comments on commit 0389eda

Please sign in to comment.