1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- <?php
- namespace Sonata\AdminBundle\Form\DataTransformer;
- use Sonata\AdminBundle\Model\ModelManagerInterface;
- use Symfony\Component\Form\DataTransformerInterface;
- class ModelToIdTransformer implements DataTransformerInterface
- {
-
- protected $modelManager;
-
- protected $className;
-
- public function __construct(ModelManagerInterface $modelManager, $className)
- {
- $this->modelManager = $modelManager;
- $this->className = $className;
- }
-
- public function reverseTransform($newId)
- {
- if (empty($newId) && !in_array($newId, array('0', 0), true)) {
- return;
- }
- return $this->modelManager->find($this->className, $newId);
- }
-
- public function transform($entity)
- {
- if (empty($entity)) {
- return;
- }
- return $this->modelManager->getNormalizedIdentifier($entity);
- }
- }
|