-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Form] DoctrineType, Add widget property to avoid retrieving the whole ChoiceList #3095
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
064cdd3
Add widget property to allow text field for the entitytype without ha…
RapotOR 61d33ae
Fix spaces & phpdoc
RapotOR 7d24d27
Remove unused property
RapotOR b70905d
Renamed the datatransformer. Replace query_builder by generic entityL…
RapotOR 3c716b9
Remove old datatransformer
RapotOR 645820d
Fix unecessary whitespaces, phpdoc, loader issues
RapotOR File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
201 changes: 201 additions & 0 deletions
201
...Symfony/Bridge/Doctrine/Form/DataTransformer/EntityToIdentifierAndPropertyTransformer.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
<?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\Doctrine\Form\DataTransformer; | ||
|
||
use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityLoaderInterface; | ||
use Symfony\Component\Form\DataTransformerInterface; | ||
use Symfony\Component\Form\Exception\UnexpectedTypeException; | ||
use Symfony\Component\Form\Exception\TransformationFailedException; | ||
use Symfony\Component\Form\Exception\FormException; | ||
use Symfony\Component\Form\Util\PropertyPath; | ||
use Doctrine\Common\Collections\Collection; | ||
use Doctrine\Common\Persistence\ObjectManager; | ||
use Doctrine\ORM\NoResultException; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unused use statement |
||
|
||
class EntityToIdentifierAndPropertyTransformer implements DataTransformerInterface | ||
{ | ||
/** | ||
* @var ObjectManager | ||
*/ | ||
private $em; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $class; | ||
|
||
/** | ||
* @var \Doctrine\Common\Persistence\Mapping\ClassMetadata | ||
*/ | ||
private $classMetadata; | ||
|
||
/** | ||
* Contains the query builder that builds the query for fetching the | ||
* entities | ||
* | ||
* This property should only be accessed through queryBuilder. | ||
* | ||
* @var EntityLoaderInterface | ||
*/ | ||
private $entityLoader; | ||
|
||
/** | ||
* The fields of which the identifier of the underlying class consists | ||
* | ||
* This property should only be accessed through identifier. | ||
* | ||
* @var array | ||
*/ | ||
private $identifier = array(); | ||
|
||
/** | ||
* Property path to access the property value. | ||
* | ||
* @var PropertyPath | ||
*/ | ||
private $propertyPath; | ||
|
||
/** | ||
* Property field name | ||
* | ||
* @var string | ||
*/ | ||
private $property; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param ObjectManager $manager An EntityManager instance | ||
* @param string $class The class name | ||
* @param array $identifier The fields of which the identifier of the underlying class consists | ||
* @param string $property The property name | ||
* @param EntityLoaderInterface $entityLoader An optional query builder | ||
*/ | ||
public function __construct(ObjectManager $manager, $class, $identifier, $property = null, EntityLoaderInterface $entityLoader = null) | ||
{ | ||
$this->em = $manager; | ||
$this->class = $class; | ||
$this->property = $property; | ||
$this->classMetadata = $this->em->getClassMetadata($class); | ||
$this->entityLoader = $entityLoader; | ||
$this->identifier = $identifier; | ||
|
||
// The property option defines, which property (path) is used for | ||
// displaying entities as strings | ||
if ($property) { | ||
$this->propertyPath = new PropertyPath($property); | ||
} elseif (!method_exists($this->classMetadata->getName(), '__toString')) { | ||
// Otherwise expect a __toString() method in the entity | ||
throw new FormException('Entities passed to the choice field must have a "__toString()" method defined (or you can also override the "property" option).'); | ||
} | ||
} | ||
|
||
/** | ||
* Transforms entities into choice keys. | ||
* | ||
* @param object a single entity or NULL | ||
* | ||
* @return mixed An array of choice keys, a single key or NULL | ||
*/ | ||
public function transform($entity) | ||
{ | ||
if (null === $entity || '' === $entity) { | ||
return array(); | ||
} | ||
|
||
if (!is_object($entity)) { | ||
throw new UnexpectedTypeException($entity, 'object'); | ||
} | ||
|
||
if ($entity instanceof Collection) { | ||
throw new \InvalidArgumentException('Expected an object, but got a collection.'); | ||
} | ||
|
||
$values = array(current($this->identifier) => current($this->getIdentifierValues($entity))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. according to the phpdoc of your constructor, |
||
|
||
if ($this->property) { | ||
$values[$this->property] = $this->propertyPath->getValue($entity); | ||
} | ||
|
||
return $values; | ||
} | ||
|
||
/** | ||
* Transforms choice keys into entities. | ||
* | ||
* @param mixed $key An array of keys, a single key or NULL | ||
* | ||
* @return object a single entity or NULL | ||
*/ | ||
public function reverseTransform($value) | ||
{ | ||
if (null === $value) { | ||
return null; | ||
} | ||
|
||
if (!is_array($value)) { | ||
throw new UnexpectedTypeException($value, 'array'); | ||
} | ||
|
||
if (implode('', $value) === '') { | ||
return null; | ||
} | ||
|
||
if (count($this->identifier) > 1 && !is_numeric($key)) { | ||
throw new UnexpectedTypeException($key, 'numeric'); | ||
} | ||
|
||
$id = current($this->identifier); | ||
|
||
if (isset($value[$id]) && !ctype_digit($value[$id]) && !is_int($value[$id])) { | ||
throw new TransformationFailedException('Identifier is invalid'); | ||
} | ||
|
||
$key = $value[$id]; | ||
|
||
if(null === $key) { | ||
return null; | ||
} | ||
|
||
if ($loader = $this->entityLoader) { | ||
$entity = $loader->getEntity(current($this->identifier), $key); | ||
} else { | ||
$entity = $this->em->find($this->class, $key); | ||
} | ||
|
||
return $entity; | ||
} | ||
|
||
/** | ||
* Returns the values of the identifier fields of an entity. | ||
* | ||
* Doctrine must know about this entity, that is, the entity must already | ||
* be persisted or added to the identity map before. Otherwise an | ||
* exception is thrown. | ||
* | ||
* @param object $entity The entity for which to get the identifier | ||
* | ||
* @return array The identifier values | ||
* | ||
* @throws FormException If the entity does not exist in Doctrine's identity map | ||
*/ | ||
public function getIdentifierValues($entity) | ||
{ | ||
if (!$this->em->contains($entity)) { | ||
throw new FormException('Entities passed to the choice field must be managed'); | ||
} | ||
|
||
$this->em->initializeObject($entity); | ||
|
||
return $this->classMetadata->getIdentifierValues($entity); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you should not have ORM specific code in this class, and so no use statements from the ORM