Symfony2嵌入表单和数据库重复

I've got a form UserType related to the entity User. This form calls another form : CityType

<?php

namespace ...\SiteBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class UserType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('city', new CityType(), array('label' => false))
                ->add(...);
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'FindBack\SiteBundle\Entity\User',
            'cascade_validation' => true
        ));
    }
    // ...
}

The User associated table looks like :

id | city_id | ...
---|---------|-----
1  | 1       | ...
2  | 1       | ...
3  | 2       | ...

Here is CityType :

<?php

namespace FindBack\SiteBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class CityType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name', 'text');
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'FindBack\SiteBundle\Entity\City',
        ));
    }

    public function getName()
    {
        return 'city';
    }
}

So when I create User through UserType form, a new City is created. If I create User twice with the same city name (ie: Marseille), my database will look like :

id  name
--|----------
1 | Marseille
2 | Marseille
3 | Paris
...

So there's a duplicate and I want to avoid it ! How can I proceed ? I tried to set an unique constraint to the name field of the City table but when I want to create another User, a "SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'Marseille' for key 'name' is thrown by Symfony2. It seems logic.

Do have I to check in the controller associated to User if there's already a city named "Marseille" in the database, and then associate the existant City to my User ?

First of all you need:

  • a controller where you'll post your form
  • a repository for your city entity

This is only an example as I don't know pretty much nothing about your application:

use Symfony\Component\HttpFoundation\Request;

public function registerUserAction(Request $request) {
  [...]
  $cr = $this->getDoctrine()->getManager()->getRepository('CityRepository');
  [...]
  $user = new User();
  $form = $this->createForm(new $form(...), $user);
  $form->bind($request);
  //get the field from form
  $cn = $request->get('city_name');
  //search for a pre existent entity
  $city = $cn->findOneByCityName($cn);
  if($city) {
    $user->setCity($city);
  }
  if($form->isValid()){
    [...]
  }
}