I'm working on an article editor in Symfony with built-in tagging capability:
The controller
class MainController extends Controller
{
public function indexAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
// $article = ...
$form = $this->createForm(new ArticleType(), $article);
$form->handleRequest($request);
if ($form->isValid()) {
$em->persist($article);
$em->flush();
return $this->redirect($this->generateUrl('acme_edit_success'));
}
return $this->render('AcmeBundle:Main:index.html.twig', array(
'form' => $form->createView()
));
}
}
The forms
The tag form is registered as a service with the @Doctrine
argument, so I can use the entity manager inside the class. The tag form is embed inside the article form.
ArticleType.php
class ArticleType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('content')
->add('tags', 'collection', array(
'type' => 'acme_bundle_tagtype',
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false
))
->add('save', 'submit')
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\Bundle\Entity\Article',
'cascade_validation' => true
));
}
public function getName()
{
return 'acme_bundle_articletype';
}
}
TagType.php
class TagType extends AbstractType
{
private $entityManager;
public function buildForm(FormBuilderInterface $builder, array $options)
{
$transformer = new TagTransformer($this->entityManager);
$builder->add(
$builder->create('name')
->addModelTransformer($transformer)
);
}
function __construct(\Doctrine\Bundle\DoctrineBundle\Registry $doctrine) {
$this->entityManager = $doctrine->getManager();
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\Bundle\Entity\Tag'
));
}
public function getName()
{
return 'acme_bundle_tagtype';
}
}
The data transformer
I created this data transformer to check if the given tag already exists and then transform the tag object to the one that already exists in the database:
class TagTransformer implements DataTransformerInterface
{
/**
* @var ObjectManager
*/
private $om;
/**
* @param ObjectManager $om
*/
public function __construct(ObjectManager $om)
{
$this->om = $om;
}
public function transform($tag)
{
if (null === $tag) {
return '';
}
return $tag;
}
public function reverseTransform($name)
{
if (!$name)
return null;
$tag = $this->om
->getRepository('AcmeBundle:Tag')
->findOneByName($name)
;
if (!$tag) {
$tag = new Tag();
$tag->setName($name);
}
return $tag;
}
}
When I try to save an article with an already existing tag, the reverseTransform()
function successfully returns the original tag objects, but the DBAL converts the object back to a string by its __toString()
method, and Doctrine still initiates an INSERT
query instead of UPDATE
, so I get the next error:
An exception occurred while executing 'INSERT INTO Tag (name) VALUES (?)' with params [{}]:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'An Existing Tag' for key 'UNIQ_0123456789ABCDE'
How can I fix this? When I enter a tag name that's already in use, I want Symfony to use the same tag in the article-tag relationship. The entity classes appear in my previous question about how to avoid duplicate entries in a many-to-many relationship with Doctrine.
There's one mistake in your transformer. Instead of checking if name is null you should verify if a tag was returned:
You also don't need to persist the tag since by default doctrine will cascade persist all the related entities.
Full method: