merged branch willdurand/fix-propel-bridge (PR #3329)

Commits
-------

88b826d [Propel] Fixed typo, removed useless use statement, used getData() instead of casting a PropelCollection
46d28cd [Propel] Fixed the CollectionToArray transformer
1f20fb1 [Propel] Removed useless code
3910735 [Propel] Avoid to duplicate objects
d69144c [Propel] Refactored the CollectionToArray transformer
1706671 [Propel] Fixed naming to reflect Doctrine bridge
1f277df [Propel] Removed useless ModelToIdTransformer

Discussion
----------

Cleaned the propel bridge (+ fixes)

I've fixed the `ModelChoiceList` with `multiple=true`, and I removed useless code.

This PR will ensure everything works fine, but it requires the following fix for Propel: https://github.com/propelorm/Propel/pull/286.

---------------------------------------------------------------------------

by willdurand at 2012-02-11T20:04:10Z

@cedriclombardot this PR will fix your issues with Sf2 + Propel in your admingen
@bschussek nevermind my comments on Twitter, it seems ok now
This commit is contained in:
Fabien Potencier 2012-02-12 00:06:59 +01:00
commit a899548d79
5 changed files with 83 additions and 193 deletions

View File

@ -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 <william.durand1@gmail.com>
*/
@ -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());
}

View File

@ -0,0 +1,56 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <william.durand1@gmail.com>
* @author Pierre-Yves Lebecq <py.lebecq@gmail.com>
*/
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;
}
}

View File

@ -1,72 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <william.durand1@gmail.com>
*/
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;
}
}

View File

@ -1,99 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <william.durand1@gmail.com>
* @author Pierre-Yves Lebecq <py.lebecq@gmail.com>
*/
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;
}
}

View File

@ -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']
);
}