Skip to content

Instantly share code, notes, and snippets.

@faboo03
Created February 26, 2015 13:34
Show Gist options
  • Save faboo03/8ee8a22b72c6cb34bb99 to your computer and use it in GitHub Desktop.
Save faboo03/8ee8a22b72c6cb34bb99 to your computer and use it in GitHub Desktop.
ModelTransformer
<?php
namespace Core\MediaBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Core\MediaBundle\Form\DataTransformer\EntityToIdentifierTransformer;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class EntityIdentifierType extends AbstractType
{
/**
* @var ObjectManager
*/
private $om;
/**
* @param ObjectManager $om
*/
public function __construct(ObjectManager $om)
{
$this->om = $om;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$transformer = new EntityToIdentifierTransformer($this->om, $options['class']);
$builder->addModelTransformer($transformer);
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
// messages
'invalid_message' => 'The selected entity does not exist',
));
$resolver->setRequired(array(
'class'
));
}
public function getParent()
{
return 'text';
}
public function getName()
{
return 'entity_id';
}
}
<?php
namespace Core\MediaBundle\Form\DataTransformer;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Doctrine\Common\Persistence\ObjectManager;
use Acme\TaskBundle\Entity\Entity;
class EntityToIdentifierTransformer implements DataTransformerInterface
{
/**
* @var ObjectManager
*/
private $om;
/**
* @var string
*/
private $classname;
/**
* @param ObjectManager $om
* @param string $classname
*/
public function __construct(ObjectManager $om, $classname)
{
$this->om = $om;
$this->classname = $classname;
}
/**
* Transforms an object (entity) to a string (id).
*
* @param Entity|null $entity
* @return string
*/
public function transform($entity)
{
if (null === $entity) {
return "";
}
return $entity->getId();
}
/**
* Transforms a string (id) to an object (entity).
*
* @param string $id
* @return Entity|null
* @throws TransformationFailedException if object (entity) is not found.
*/
public function reverseTransform($id)
{
if (!$id) {
return null;
}
$entity = $this->om
->getRepository($this->classname)
->find($id)
;
if (null === $entity) {
throw new TransformationFailedException(sprintf(
'The entity with "%s" id could not be found',
$id
));
}
return $entity;
}
}
parameters:
# core_media.example.class: Core\MediaBundle\Example
services:
# core_media.example:
# class: %core_media.example.class%
# arguments: [@service_id, "plain_value", %parameter%]
core_media.type.entity_id:
class: Core\MediaBundle\Form\Type\EntityIdentifierType
arguments: ["@doctrine.orm.entity_manager"]
tags:
- { name: form.type, alias: entity_id }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment