-
-
Save bjo3rnf/4061232 to your computer and use it in GitHub Desktop.
<?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; | |
public function __construct(ObjectManager $objectManager, $class) | |
{ | |
$this->objectManager = $objectManager; | |
$this->class = $class; | |
} | |
public function transform($entity) | |
{ | |
if (null === $entity) { | |
return; | |
} | |
return $entity->getId(); | |
} | |
public function reverseTransform($id) | |
{ | |
if (!$id) { | |
return null; | |
} | |
$entity = $this->objectManager | |
->getRepository($this->class) | |
->find($id); | |
if (null === $entity) { | |
throw new TransformationFailedException(); | |
} | |
return $entity; | |
} | |
} |
Worked like a charm! Thanks!
Remember add the service in the bundle services.yml to allow use the new field type
services:
acme_demo.type.entity_hidden:
class: Acme\DemoBundle\Form\Type\EntityHiddenType
arguments: ["@doctrine.orm.entity_manager"]
tags:
- { name: form.type, alias: entity_hidden }
add the services.yml to the config.yml imports:
imports:
- { resource: "@AcmeDemoBundle/Resources/config/services.yml" }
Modify the namespaces to fit with your bundle structure
and just add a new "entity_hidden" field to the form in this way:
$builder
->add('email')
->add('name')
->add('car', 'entity_hidden', array(
'class' => 'Acme\DemoBundle\Entity\Car'
))
More info about apply the DataTransformer in the symfony2 Docs:
http://symfony.com/doc/current/cookbook/form/data_transformers.html#creating-the-transformer
It worked!, thanks @dreipunktnull
It worked for me as well, thanks.
I found this very useful - thanks!
Thanks for sharing this, it helped a lot!
Thanks! very useful...
Thanks, is fine!
Do you guess this works the same if I only change the argument for ODM? Meaning, changing this in the configuration of the service: arguments: ["@doctrine.odm.mongodb.document_manager"]
Excuse my questions, im new to symfony. How can i pass some options to the form since i used this in controller?
private function createEditForm(Sms $entity)
{
$form = $this->createForm(new SmsType(), $entity, array(
'action' => $this->generateUrl('sms_update', array('id' => $entity->getId())),
'method' => 'PUT',
));
$form->add('submit', 'submit', array('label' => 'Update'));
return $form;
}
Thx 4 answering. I need to pass user entity
Thx found what i was looking for. Thought that only instance of AbstractType could be passed in
$this->createForm('acme_userbundle_sms', $entity, array(
'action' => $this->generateUrl('sms_create'),
'method' => 'POST',
));
```php
form_name 'acme_userbundle_sms' could be passed. Thks for your time
Thanks, dude =)
- 1 cheers !
Thanks!!! Great Job!!!
If the user doesn't write the class option:
$builder
->add('car', 'hidden_entity', array())
;
the class is null and the transformation throws the next error:
Class '' does not exist
If the user uses the method setRequired() in EntityHiddenType file:
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver
->setRequired(array('class'))
->setDefaults(array(
'invalid_message' => 'The entity does not exist.',
))
;
}
the application throws a 500 error with the next information:
The required option "class" is missing.
Hi @jamogon,
thanks for the hint. I updated the Gist accordingly. Didn't even know about the setRequired()
method of the OptionsResolver.
Cheers
Björn
Great Job !!
Thank you !
Why Symfony community would not integrate this solution. it's very useful !
👍
Did you ever propose this improvement to Symfony, @bjo3rnf ?
There also seems to be a simple workaround using the property_path
:
$builder->add('entity', 'hidden', array('property_path' => 'entity.id'));
as mentioned by @webmozart here:
symfony/symfony#3182
And it looks like it is finally going to make it into symfony here:
symfony/symfony#8293
Thank you! Works great!
How to pass default value to Hidden_entity field? for eg. I have test controller which has field Author. Author I am getting from db after user logs in to system.
How can I set an entity in a onPreSubmit or preSetData and have it attach to the form entity?
Thanks! It helped a lot!
I forked it to make it compatible with symfony 2.8 ( https://gist.github.com/xanou/e630e4e25df60d33b050 )
Thanks, you made my day!
Great job!
One question, how do I pass the class?