Skip to content

Commit

Permalink
Merge pull request #3 from ismailbaskin/master
Browse files Browse the repository at this point in the history
Use property accessor component for extensible textProperty
  • Loading branch information
tetranz committed Oct 7, 2015
2 parents 62fbf5d + 45cfef2 commit df1b044
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
7 changes: 5 additions & 2 deletions Form/DataTransformer/EntitiesToPropertyTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\PropertyAccess\PropertyAccess;

/**
* Data transformer for multiple mode (i.e., multiple = true)
Expand Down Expand Up @@ -40,14 +41,16 @@ public function transform($entities)
// return an array of initial values as html encoded json
$data = array();

$accessor = PropertyAccess::createPropertyAccessor();

foreach($entities as $entity) {

$text = is_null($this->textProperty)
? (string) $entity
: $entity->{'get' . $this->textProperty}();
: $accessor->getValue($entity, $this->textProperty);

$data[] = array(
'id' => $entity->getId(),
'id' => $accessor->getValue($entity, 'id'),
'text' => $text
);
}
Expand Down
10 changes: 7 additions & 3 deletions Form/DataTransformer/EntityToPropertyTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Doctrine\ORM\EntityManager;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\PropertyAccess\PropertyAccess;

/**
* Data transformer for single mode (i.e., multiple = false)
Expand Down Expand Up @@ -36,14 +37,16 @@ public function transform($entity)
return '';
}

$accessor = PropertyAccess::createPropertyAccessor();

// return the initial values as html encoded json

$text = is_null($this->textProperty)
? (string) $entity
: $entity->{'get' . $this->textProperty}();
? (string)$entity
: $accessor->getValue($entity, $this->textProperty);

$data = array(
'id' => $entity->getId(),
'id' => $accessor->getValue($entity, 'id'),
'text' => $text
);

Expand All @@ -63,6 +66,7 @@ public function reverseTransform($value)
}

$repo = $this->em->getRepository($this->className);

return $repo->find($value);
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
}
],
"require": {
"symfony/symfony": ">=2.2"
"symfony/symfony": ">=2.2",
"doctrine/orm": ">=2.2"
},
"autoload": {
"psr-0": { "Tetranz\\Select2EntityBundle": "" }
Expand Down

0 comments on commit df1b044

Please sign in to comment.