Skip to content

Commit

Permalink
[Propel] Refactored the CollectionToArray transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
willdurand committed Feb 9, 2012
1 parent 1706671 commit d69144c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 43 deletions.
23 changes: 19 additions & 4 deletions src/Symfony/Bridge/Propel1/Form/ChoiceList/ModelChoiceList.php
Expand Up @@ -15,6 +15,8 @@
use Symfony\Component\Form\Exception\StringCastException;
use Symfony\Component\Form\Extension\Core\ChoiceList\ObjectChoiceList;

use \Persistent;

/**
* Widely inspirated by the EntityChoiceList (Symfony2).
*
Expand Down Expand Up @@ -78,6 +80,16 @@ public function __construct($class, $labelPath = null, $choices = null, $queryOb
parent::__construct($choices, $labelPath, array(), $groupPath);
}

/**
* Returns the class name
*
* @return string
*/
public function getClass()
{
return $this->class;
}

/**
* Returns the list of model objects
*
Expand Down Expand Up @@ -157,7 +169,11 @@ public function getChoicesForValues(array $values)
{
if (!$this->loaded) {
if (1 === count($this->identifier)) {
return $this->query->create()->filterBy(current($this->identifier), $values)->findOne();
$filterBy = 'filterBy' . current($this->identifier)->getPhpName();

return (array) $this->query->create()
->$filterBy($values)
->find();
}

$this->load();
Expand All @@ -184,7 +200,6 @@ public function getValuesForChoices(array $models)
// Attention: This optimization does not check choices for existence
if (1 === count($this->identifier)) {
$values = array();

foreach ($models as $model) {
if ($model instanceof $this->class) {
// Make sure to convert to the right format
Expand Down Expand Up @@ -308,7 +323,7 @@ protected function createValue($model)
*/
private function load()
{
$models = $this->query->find();
$models = (array) $this->query->find();

try {
// The second parameter $labels is ignored by ObjectChoiceList
Expand All @@ -333,7 +348,7 @@ private function load()
*/
private function getIdentifierValues($model)
{
if ($model instanceof \Persistent) {
if ($model instanceof Persistent) {
return array($model->getPrimaryKey());
}

Expand Down
Expand Up @@ -28,13 +28,10 @@
class CollectionToArrayTransformer implements DataTransformerInterface
{
/**
* @var \Propel\PropelBundle\Form\ChoiceList\ModelChoiceList
* @var \Symfony\Bridge\Propel1\Form\ChoiceList\ModelChoiceList
*/
private $choiceList;

/**
* @param \Propel\PropelBundle\Form\ChoiceList\ModelChoiceList $choiceList
*/
public function __construct(ModelChoiceList $choiceList)
{
$this->choiceList = $choiceList;
Expand All @@ -50,49 +47,25 @@ public function transform($collection)
throw new UnexpectedTypeException($collection, '\PropelCollection');
}

$array = array();

if (count($this->choiceList->getIdentifier()) > 1) {
$availableModels = $this->choiceList->getModels();

foreach ($collection as $model) {
$key = array_search($model, $availableModels);
$array[] = $key;
}
} else {
foreach ($collection as $model) {
$array[] = current($this->choiceList->getIdentifierValues($model));
}
}
$collection->setModel($this->choiceList->getClass());

return $array;
return $collection->toArray();
}

public function reverseTransform($keys)
public function reverseTransform($array)
{
$collection = new PropelObjectCollection();

if ('' === $keys || null === $keys) {
if ('' === $array || null === $array) {
return $collection;
}

if (!is_array($keys)) {
throw new UnexpectedTypeException($keys, 'array');
if (!is_array($array)) {
throw new UnexpectedTypeException($array, 'array');
}

$notFound = array();

foreach ($keys as $key) {
if ($model = $this->choiceList->getModel($key)) {
$collection->append($model);
} else {
$notFound[] = $key;
}
}

if (count($notFound) > 0) {
throw new TransformationFailedException(sprintf('The models with keys "%s" could not be found', implode('", "', $notFound)));
}
$collection->setModel($this->choiceList->getClass());
$collection->fromArray($array);

return $collection;
}
Expand Down
8 changes: 5 additions & 3 deletions src/Symfony/Bridge/Propel1/Form/Type/ModelType.php
Expand Up @@ -13,6 +13,7 @@

use Symfony\Bridge\Propel1\Form\ChoiceList\ModelChoiceList;
use Symfony\Bridge\Propel1\Form\DataTransformer\CollectionToArrayTransformer;
use Symfony\Bridge\Propel1\Form\DataTransformer\ObjectToArrayTransformer;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

Expand All @@ -39,8 +40,8 @@ public function getDefaultOptions(array $options)
'class' => null,
'property' => null,
'query' => null,
'choices' => array(),
'preferred_choices' => array(),
'choices' => null,
'group_by' => null,
);

$options = array_replace($defaultOptions, $options);
Expand All @@ -50,7 +51,8 @@ public function getDefaultOptions(array $options)
$options['class'],
$options['property'],
$options['choices'],
$options['query']
$options['query'],
$options['group_by']
);
}

Expand Down

0 comments on commit d69144c

Please sign in to comment.