Created
September 7, 2017 08:49
-
-
Save adamsafr/b0ed3a467b519de88262fc7119718f52 to your computer and use it in GitHub Desktop.
[php] Hidden field for Symfony2 entities
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
<?php | |
namespace Dpn\ToolsBundle\Form\DataTransformer; | |
use Symfony\Component\Form\DataTransformerInterface; | |
use Symfony\Component\Form\Exception\TransformationFailedException; | |
use Doctrine\Common\Persistence\ObjectManager; | |
class EntityToIdTransformer implements DataTransformerInterface | |
{ | |
/** | |
* @var ObjectManager | |
*/ | |
protected $objectManager; | |
/** | |
* @var string | |
*/ | |
protected $class; | |
/** | |
* Constructor | |
* @param ObjectManager $objectManager | |
* @param string $class | |
*/ | |
public function __construct(ObjectManager $objectManager, $class) | |
{ | |
$this->objectManager = $objectManager; | |
$this->class = $class; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function transform($entity) | |
{ | |
if (null === $entity) { | |
return; | |
} | |
return $entity->getId(); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function reverseTransform($id) | |
{ | |
if (!$id) { | |
return null; | |
} | |
$entity = $this->objectManager | |
->getRepository($this->class) | |
->find($id); | |
if (null === $entity) { | |
throw new TransformationFailedException(); | |
} | |
return $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
<?php | |
namespace Dpn\ToolsBundle\Form\Type; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\OptionsResolver\OptionsResolver; | |
use Dpn\ToolsBundle\Form\Type\EntityHiddenType; | |
class ExampleType extends AbstractType | |
{ | |
/** | |
* @param FormBuilderInterface $builder | |
* @param array $options | |
*/ | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
$builder | |
->add('category', EntityHiddenType::class, array( | |
'label' => false, | |
'class' => 'Dpn\ToolsBundle\Entity\Category' | |
)) | |
; | |
} | |
/** | |
* @param OptionsResolver $resolver | |
*/ | |
public function configureOptions(OptionsResolver $resolver) | |
{ | |
$resolver->setDefaults(array( | |
'data_class' => 'Tma\MainBundle\Entity\Example', | |
)); | |
} | |
} |
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
services: | |
dpn.type.entity_hidden: | |
class: Dpn\ToolsBundle\Form\Type\EntityHiddenType | |
arguments: ["@doctrine.orm.entity_manager"] | |
tags: | |
- { name: form.type } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment