[Propel] Refactored the CollectionToArray transformer

This commit is contained in:
William DURAND 2012-02-09 10:59:13 +01:00
parent 1706671159
commit d69144c614
3 changed files with 33 additions and 43 deletions

View File

@ -15,6 +15,8 @@ use Symfony\Component\Form\Exception\FormException;
use Symfony\Component\Form\Exception\StringCastException; use Symfony\Component\Form\Exception\StringCastException;
use Symfony\Component\Form\Extension\Core\ChoiceList\ObjectChoiceList; use Symfony\Component\Form\Extension\Core\ChoiceList\ObjectChoiceList;
use \Persistent;
/** /**
* Widely inspirated by the EntityChoiceList (Symfony2). * Widely inspirated by the EntityChoiceList (Symfony2).
* *
@ -78,6 +80,16 @@ class ModelChoiceList extends ObjectChoiceList
parent::__construct($choices, $labelPath, array(), $groupPath); parent::__construct($choices, $labelPath, array(), $groupPath);
} }
/**
* Returns the class name
*
* @return string
*/
public function getClass()
{
return $this->class;
}
/** /**
* Returns the list of model objects * Returns the list of model objects
* *
@ -157,7 +169,11 @@ class ModelChoiceList extends ObjectChoiceList
{ {
if (!$this->loaded) { if (!$this->loaded) {
if (1 === count($this->identifier)) { 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(); $this->load();
@ -184,7 +200,6 @@ class ModelChoiceList extends ObjectChoiceList
// Attention: This optimization does not check choices for existence // Attention: This optimization does not check choices for existence
if (1 === count($this->identifier)) { if (1 === count($this->identifier)) {
$values = array(); $values = array();
foreach ($models as $model) { foreach ($models as $model) {
if ($model instanceof $this->class) { if ($model instanceof $this->class) {
// Make sure to convert to the right format // Make sure to convert to the right format
@ -308,7 +323,7 @@ class ModelChoiceList extends ObjectChoiceList
*/ */
private function load() private function load()
{ {
$models = $this->query->find(); $models = (array) $this->query->find();
try { try {
// The second parameter $labels is ignored by ObjectChoiceList // The second parameter $labels is ignored by ObjectChoiceList
@ -333,7 +348,7 @@ class ModelChoiceList extends ObjectChoiceList
*/ */
private function getIdentifierValues($model) private function getIdentifierValues($model)
{ {
if ($model instanceof \Persistent) { if ($model instanceof Persistent) {
return array($model->getPrimaryKey()); return array($model->getPrimaryKey());
} }

View File

@ -28,13 +28,10 @@ use \PropelObjectCollection;
class CollectionToArrayTransformer implements DataTransformerInterface class CollectionToArrayTransformer implements DataTransformerInterface
{ {
/** /**
* @var \Propel\PropelBundle\Form\ChoiceList\ModelChoiceList * @var \Symfony\Bridge\Propel1\Form\ChoiceList\ModelChoiceList
*/ */
private $choiceList; private $choiceList;
/**
* @param \Propel\PropelBundle\Form\ChoiceList\ModelChoiceList $choiceList
*/
public function __construct(ModelChoiceList $choiceList) public function __construct(ModelChoiceList $choiceList)
{ {
$this->choiceList = $choiceList; $this->choiceList = $choiceList;
@ -50,49 +47,25 @@ class CollectionToArrayTransformer implements DataTransformerInterface
throw new UnexpectedTypeException($collection, '\PropelCollection'); throw new UnexpectedTypeException($collection, '\PropelCollection');
} }
$array = array(); $collection->setModel($this->choiceList->getClass());
if (count($this->choiceList->getIdentifier()) > 1) { return $collection->toArray();
$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));
}
}
return $array;
} }
public function reverseTransform($keys) public function reverseTransform($array)
{ {
$collection = new PropelObjectCollection(); $collection = new PropelObjectCollection();
if ('' === $keys || null === $keys) { if ('' === $array || null === $array) {
return $collection; return $collection;
} }
if (!is_array($keys)) { if (!is_array($array)) {
throw new UnexpectedTypeException($keys, 'array'); throw new UnexpectedTypeException($array, 'array');
} }
$notFound = array(); $collection->setModel($this->choiceList->getClass());
$collection->fromArray($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)));
}
return $collection; return $collection;
} }

View File

@ -13,6 +13,7 @@ namespace Symfony\Bridge\Propel1\Form\Type;
use Symfony\Bridge\Propel1\Form\ChoiceList\ModelChoiceList; use Symfony\Bridge\Propel1\Form\ChoiceList\ModelChoiceList;
use Symfony\Bridge\Propel1\Form\DataTransformer\CollectionToArrayTransformer; use Symfony\Bridge\Propel1\Form\DataTransformer\CollectionToArrayTransformer;
use Symfony\Bridge\Propel1\Form\DataTransformer\ObjectToArrayTransformer;
use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder; use Symfony\Component\Form\FormBuilder;
@ -39,8 +40,8 @@ class ModelType extends AbstractType
'class' => null, 'class' => null,
'property' => null, 'property' => null,
'query' => null, 'query' => null,
'choices' => array(), 'choices' => null,
'preferred_choices' => array(), 'group_by' => null,
); );
$options = array_replace($defaultOptions, $options); $options = array_replace($defaultOptions, $options);
@ -50,7 +51,8 @@ class ModelType extends AbstractType
$options['class'], $options['class'],
$options['property'], $options['property'],
$options['choices'], $options['choices'],
$options['query'] $options['query'],
$options['group_by']
); );
} }