diff --git a/src/Symfony/Bridge/Propel1/Form/ChoiceList/ModelChoiceList.php b/src/Symfony/Bridge/Propel1/Form/ChoiceList/ModelChoiceList.php index fe82116de6..1817dd1e29 100644 --- a/src/Symfony/Bridge/Propel1/Form/ChoiceList/ModelChoiceList.php +++ b/src/Symfony/Bridge/Propel1/Form/ChoiceList/ModelChoiceList.php @@ -11,12 +11,13 @@ namespace Symfony\Bridge\Propel1\Form\ChoiceList; +use \Persistent; use Symfony\Component\Form\Exception\FormException; use Symfony\Component\Form\Exception\StringCastException; use Symfony\Component\Form\Extension\Core\ChoiceList\ObjectChoiceList; /** - * Widely inspirated by the EntityChoiceList (Symfony2). + * Widely inspirated by the EntityChoiceList. * * @author William Durand */ @@ -31,13 +32,6 @@ class ModelChoiceList extends ObjectChoiceList */ private $identifier = array(); - /** - * TableMap - * - * @var \TableMap - */ - private $table = null; - /** * Query */ @@ -64,8 +58,7 @@ class ModelChoiceList extends ObjectChoiceList $queryClass = $this->class . 'Query'; $query = new $queryClass(); - $this->table = $query->getTableMap(); - $this->identifier = $this->table->getPrimaryKeys(); + $this->identifier = $query->getTableMap()->getPrimaryKeys(); $this->query = $queryObject ?: $query; $this->loaded = is_array($choices) || $choices instanceof \Traversable; @@ -78,6 +71,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 +160,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 +191,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 +314,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 +339,7 @@ class ModelChoiceList extends ObjectChoiceList */ private function getIdentifierValues($model) { - if ($model instanceof \Persistent) { + if ($model instanceof Persistent) { return array($model->getPrimaryKey()); } diff --git a/src/Symfony/Bridge/Propel1/Form/DataTransformer/CollectionToArrayTransformer.php b/src/Symfony/Bridge/Propel1/Form/DataTransformer/CollectionToArrayTransformer.php new file mode 100644 index 0000000000..98b4e8f19e --- /dev/null +++ b/src/Symfony/Bridge/Propel1/Form/DataTransformer/CollectionToArrayTransformer.php @@ -0,0 +1,56 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Propel1\Form\DataTransformer; + +use \PropelCollection; +use Symfony\Component\Form\DataTransformerInterface; +use Symfony\Component\Form\Exception\UnexpectedTypeException; +use Symfony\Component\Form\Exception\TransformationFailedException; + +/** + * CollectionToArrayTransformer class. + * + * @author William Durand + * @author Pierre-Yves Lebecq + */ +class CollectionToArrayTransformer implements DataTransformerInterface +{ + public function transform($collection) + { + if (null === $collection) { + return array(); + } + + if (!$collection instanceof PropelCollection) { + throw new UnexpectedTypeException($collection, '\PropelCollection'); + } + + return $collection->getData(); + } + + public function reverseTransform($array) + { + $collection = new PropelCollection(); + + if ('' === $array || null === $array) { + return $collection; + } + + if (!is_array($array)) { + throw new UnexpectedTypeException($array, 'array'); + } + + $collection->setData($array); + + return $collection; + } +} diff --git a/src/Symfony/Bridge/Propel1/Form/DataTransformer/ModelToIdTransformer.php b/src/Symfony/Bridge/Propel1/Form/DataTransformer/ModelToIdTransformer.php deleted file mode 100644 index 08061a231f..0000000000 --- a/src/Symfony/Bridge/Propel1/Form/DataTransformer/ModelToIdTransformer.php +++ /dev/null @@ -1,72 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Bridge\Propel1\Form\DataTransformer; - -use Symfony\Bridge\Propel1\Form\ChoiceList\ModelChoiceList; -use Symfony\Component\Form\DataTransformerInterface; -use Symfony\Component\Form\Exception\UnexpectedTypeException; -use Symfony\Component\Form\Exception\TransformationFailedException; - -/** - * @author William Durand - */ -class ModelToIdTransformer implements DataTransformerInterface -{ - /** - * @var \Propel\PropelBundle\Form\ChoiceList\ModelChoiceList - */ - private $choiceList; - - /** - * @param \Propel\PropelBundle\Form\ChoiceList\ModelChoiceList $choiceList - */ - public function __construct(ModelChoiceList $choiceList) - { - $this->choiceList = $choiceList; - } - - public function transform($model) - { - if (null === $model || '' === $model) { - return ''; - } - - if (!is_object($model)) { - throw new UnexpectedTypeException($model, 'object'); - } - - if (count($this->choiceList->getIdentifier()) > 1) { - $availableModels = $this->choiceList->getModels(); - - return array_search($model, $availableModels); - } - - return current($this->choiceList->getIdentifierValues($model)); - } - - public function reverseTransform($key) - { - if ('' === $key || null === $key) { - return null; - } - - if (count($this->choiceList->getIdentifier()) > 1 && !is_numeric($key)) { - throw new UnexpectedTypeException($key, 'numeric'); - } - - if (!$model = $this->choiceList->getModel($key)) { - throw new TransformationFailedException(sprintf('The model with key "%s" could not be found', $key)); - } - - return $model; - } -} diff --git a/src/Symfony/Bridge/Propel1/Form/DataTransformer/ModelsToArrayTransformer.php b/src/Symfony/Bridge/Propel1/Form/DataTransformer/ModelsToArrayTransformer.php deleted file mode 100644 index 8996c0e1de..0000000000 --- a/src/Symfony/Bridge/Propel1/Form/DataTransformer/ModelsToArrayTransformer.php +++ /dev/null @@ -1,99 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Bridge\Propel1\Form\DataTransformer; - -use Symfony\Bridge\Propel1\Form\ChoiceList\ModelChoiceList; -use Symfony\Component\Form\DataTransformerInterface; -use Symfony\Component\Form\Exception\UnexpectedTypeException; -use Symfony\Component\Form\Exception\TransformationFailedException; - -use \PropelCollection; -use \PropelObjectCollection; - -/** - * ModelsToArrayTransformer class. - * - * @author William Durand - * @author Pierre-Yves Lebecq - */ -class ModelsToArrayTransformer implements DataTransformerInterface -{ - /** - * @var \Propel\PropelBundle\Form\ChoiceList\ModelChoiceList - */ - private $choiceList; - - /** - * @param \Propel\PropelBundle\Form\ChoiceList\ModelChoiceList $choiceList - */ - public function __construct(ModelChoiceList $choiceList) - { - $this->choiceList = $choiceList; - } - - public function transform($collection) - { - if (null === $collection) { - return array(); - } - - if (!$collection instanceof PropelCollection) { - 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)); - } - } - - return $array; - } - - public function reverseTransform($keys) - { - $collection = new PropelObjectCollection(); - - if ('' === $keys || null === $keys) { - return $collection; - } - - if (!is_array($keys)) { - throw new UnexpectedTypeException($keys, '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))); - } - - return $collection; - } -} diff --git a/src/Symfony/Bridge/Propel1/Form/Type/ModelType.php b/src/Symfony/Bridge/Propel1/Form/Type/ModelType.php index 254727da5d..741b6fd839 100644 --- a/src/Symfony/Bridge/Propel1/Form/Type/ModelType.php +++ b/src/Symfony/Bridge/Propel1/Form/Type/ModelType.php @@ -12,8 +12,7 @@ namespace Symfony\Bridge\Propel1\Form\Type; use Symfony\Bridge\Propel1\Form\ChoiceList\ModelChoiceList; -use Symfony\Bridge\Propel1\Form\DataTransformer\ModelToIdTransformer; -use Symfony\Bridge\Propel1\Form\DataTransformer\ModelsToArrayTransformer; +use Symfony\Bridge\Propel1\Form\DataTransformer\CollectionToArrayTransformer; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilder; @@ -27,9 +26,7 @@ class ModelType extends AbstractType public function buildForm(FormBuilder $builder, array $options) { if ($options['multiple']) { - $builder->prependClientTransformer(new ModelsToArrayTransformer($options['choice_list'])); - } else { - $builder->prependClientTransformer(new ModelToIdTransformer($options['choice_list'])); + $builder->prependClientTransformer(new CollectionToArrayTransformer()); } } @@ -42,8 +39,9 @@ class ModelType extends AbstractType 'class' => null, 'property' => null, 'query' => null, - 'choices' => array(), - 'preferred_choices' => array(), + 'choices' => null, + 'group_by' => null, + 'by_reference' => false, ); $options = array_replace($defaultOptions, $options); @@ -53,7 +51,8 @@ class ModelType extends AbstractType $options['class'], $options['property'], $options['choices'], - $options['query'] + $options['query'], + $options['group_by'] ); }