[Propel] Refactored the CollectionToArray transformer
This commit is contained in:
parent
1706671159
commit
d69144c614
@ -15,6 +15,8 @@ use Symfony\Component\Form\Exception\FormException;
|
||||
use Symfony\Component\Form\Exception\StringCastException;
|
||||
use Symfony\Component\Form\Extension\Core\ChoiceList\ObjectChoiceList;
|
||||
|
||||
use \Persistent;
|
||||
|
||||
/**
|
||||
* Widely inspirated by the EntityChoiceList (Symfony2).
|
||||
*
|
||||
@ -78,6 +80,16 @@ class ModelChoiceList extends ObjectChoiceList
|
||||
parent::__construct($choices, $labelPath, array(), $groupPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the class name
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getClass()
|
||||
{
|
||||
return $this->class;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the list of model objects
|
||||
*
|
||||
@ -157,7 +169,11 @@ class ModelChoiceList extends ObjectChoiceList
|
||||
{
|
||||
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();
|
||||
@ -184,7 +200,6 @@ class ModelChoiceList extends ObjectChoiceList
|
||||
// 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
|
||||
@ -308,7 +323,7 @@ class ModelChoiceList extends ObjectChoiceList
|
||||
*/
|
||||
private function load()
|
||||
{
|
||||
$models = $this->query->find();
|
||||
$models = (array) $this->query->find();
|
||||
|
||||
try {
|
||||
// The second parameter $labels is ignored by ObjectChoiceList
|
||||
@ -333,7 +348,7 @@ class ModelChoiceList extends ObjectChoiceList
|
||||
*/
|
||||
private function getIdentifierValues($model)
|
||||
{
|
||||
if ($model instanceof \Persistent) {
|
||||
if ($model instanceof Persistent) {
|
||||
return array($model->getPrimaryKey());
|
||||
}
|
||||
|
||||
|
@ -28,13 +28,10 @@ use \PropelObjectCollection;
|
||||
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;
|
||||
@ -50,49 +47,25 @@ class CollectionToArrayTransformer implements DataTransformerInterface
|
||||
throw new UnexpectedTypeException($collection, '\PropelCollection');
|
||||
}
|
||||
|
||||
$array = array();
|
||||
$collection->setModel($this->choiceList->getClass());
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
@ -13,6 +13,7 @@ namespace Symfony\Bridge\Propel1\Form\Type;
|
||||
|
||||
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;
|
||||
|
||||
@ -39,8 +40,8 @@ class ModelType extends AbstractType
|
||||
'class' => null,
|
||||
'property' => null,
|
||||
'query' => null,
|
||||
'choices' => array(),
|
||||
'preferred_choices' => array(),
|
||||
'choices' => null,
|
||||
'group_by' => null,
|
||||
);
|
||||
|
||||
$options = array_replace($defaultOptions, $options);
|
||||
@ -50,7 +51,8 @@ class ModelType extends AbstractType
|
||||
$options['class'],
|
||||
$options['property'],
|
||||
$options['choices'],
|
||||
$options['query']
|
||||
$options['query'],
|
||||
$options['group_by']
|
||||
);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user