title: Add if not exists or retrieve an entity in another entity
body: <p>Here is my problem : I have 2 entities named "Brand" and "Element". Each elements are related to a unique brand.</p>
  
  <p><strong>Brand entity</strong></p>
  
  <pre><code>/**
  * Brand
  *
  * @ORM\Entity(repositoryClass="Acme\AcmeBundle\Entity\BrandRepository")
  */
  
  class Brand
  {
      /**
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /**
       * @ORM\Column(name="name", type="string", length=255)
       */
      private $name;
  
      /**
       * @ORM\Column(name="model", type="string", length=255, nullable=true)
       */
      private $model;
  </code></pre>
  
  <p><strong>Element entity</strong></p>
  
  <pre><code>/**
   * Element
   *
   * @ORM\Entity(repositoryClass="Acme\AcmeBundle\Entity\ElementRepository")
   */
  
  class Element
  {
      /**
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /**
       * @ORM\Column(name="serial", type="string", length=255)
       */
      private $serial;
  
      ... others properties ...
  
      /**
       * @ORM\ManyToOne(targetEntity="Brand", cascade={"persist"})
       * @ORM\JoinColumn(nullable=false)
       */
      private $brand;
  </code></pre>
  
  <p>I created a single Element form which integrated the Brand form :</p>
  
  <pre><code>// src/Acme/AcmeBundle/Form/ElementType.php
  
  /**
   * @param FormBuilderInterface $builder
   * @param array $options
   */
  public function buildForm(FormBuilderInterface $builder, array $options)
  {
      $builder
          -&gt;add('brand', new BrandType())
          -&gt;add('mac')
          -&gt;add('ip')
      ;
  }
  </code></pre>
  
  <p>It works perfectly, but each new elements add a new brand, so I can have duplicates in my table (the brands 2 and 4 for example) :</p>
  
  <pre><code>-+--------+----------+-------+
   | ID | name |      model    |
  -+--------+----------+-------+
   | 1  | Dell |  Inspiron 14  |
  -+--------+----------+-------+
   | 2  | Dell |  Inspiron 15  |
  -+--------+----------+-------+
   | 3  | Acer |   Aspire E    |
  -+--------+----------+-------+
   | 4  | Dell |  Inspiron 15  |
  -+--------+----------+-------+
   | .  |  ..  |      ...      |
  -+--------+----------+-------+
  </code></pre>
  
  <p>When adding a new element, how to retrieve a registered brand (and mapped the element with it) instead of adding a new brand please ?</p>
  
  <p>I thought about calling the EntityManager (search in Brand if the couple "name+model" exists, and if it's the case return this object) in a PrePersist function, but according other people it's not a good solution.</p>
  
  <p><strong>Edit</strong></p>
  
  <p>Here is the solution I used :</p>
  
  <pre><code>// src/Acme/AcmeBundle/Controller/AcmeController.php
  
  public function createAction(Request $request)
  {
      $entity = new Element();
      $form = $this-&gt;createForm(...);
      $form-&gt;handleRequest($request);
  
      if ($form-&gt;isValid()) {
  
          $brand = $this-&gt;getDoctrine()
                 -&gt;getRepository('AcmeAcmeBundle:Brand')
                 -&gt;findOneBy(array(
                     'name' =&gt; $entity-&gt;getBrand()-&gt;getName(),
                     'model' =&gt; $entity-&gt;getBrand()-&gt;getModel()
                  ));
          if($brand)
              $entity-&gt;setBrand($brand);
  
          $em = $this-&gt;getDoctrine()-&gt;getManager();
          $em-&gt;persist($entity);
          $em-&gt;flush();
  
          // Redirect
      }
  }
  </code></pre>