1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Sonata\BaseApplicationBundle\Form\ValueTransformer;
- use Symfony\Component\Form\ValueTransformer\ValueTransformerInterface;
- use Symfony\Component\Form\ValueTransformer\TransformationFailedException;
- use Symfony\Component\Form\Configurable;
- /**
- * Transforms a Doctrine Entity into its identifier value and back.
- *
- * This only works with single-field primary key fields.
- *
- * @author Benjamin Eberlei <kontakt@beberlei.de>
- */
- class EntityToIDTransformer extends Configurable implements ValueTransformerInterface
- {
- protected function configure()
- {
- $this->addRequiredOption('em');
- $this->addRequiredOption('className');
- parent::configure();
- }
- /**
- * Reverse Transforming the selected id value to an Doctrine Entity.
- *
- * This handles NULL, the EntityManager#find method returns null if no entity was found.
- *
- * @param int|string $newId
- * @param object $oldEntity
- * @return object
- */
- public function reverseTransform($newId)
- {
- if (empty($newId)) {
- return null;
- }
- return $this->getOption('em')->find($this->getOption('className'), $newId);
- }
- /**
- * @param object $entity
- * @return int|string
- */
- public function transform($entity)
- {
- if (empty($entity)) {
- return 0;
- }
- return current( $this->getOption('em')->getUnitOfWork()->getEntityIdentifier($entity) );
- }
- }
|