title: Doctrine/Symfony2 Relationship Between 2 Entities over 2 Forms
body: <p>Current the following exists:</p>
  
  <p><strong>Listing Entity</strong></p>
  
  <pre><code>class Listing {
  
  /**
   * @var integer $id
   *
   * @ORM\Column(name="listing_id", type="integer")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  private $id;
  
  /**
   * @var string $title
   *
   * @ORM\Column(name="title", type="string", length=255)
   */
  private $title;
  
  /**
   * @ORM\ManyToOne(targetEntity="IntA\UserBundle\Entity\User", cascade ={"delete"}, inversedBy="listings")
   * @ORM\JoinColumn(onDelete="CASCADE")
   */
  protected $user;
  
  
  
   /**
   * @ORM\OneToMany(targetEntity="IntA\UploadBundle\Entity\Gallery", mappedBy="listing_gallery")     
   *
   * @var ArrayCollection $image
   */
  private $image;
  .
  .
  .
   /**
   * @ORM\ManyToMany(targetEntity="IntA\Bundle\Entity\Tag", cascade={"persist"})
   * @ORM\JoinTable(name="listing_tag",
   *     joinColumns={@ORM\JoinColumn(name="listing_id", referencedColumnName="listing_id")},
   *     inverseJoinColumns={@ORM\JoinColumn(name="tag_id", referencedColumnName="id")}
   * )
   *
   * @var ArrayCollection $tags
   */
  protected $tags; 
  </code></pre>
  
  <p><strong>Tag Entity</strong> (Exists, but code not relevant for this problem)</p>
  
  <p><strong>Gallery Entity</strong></p>
  
  <pre><code>class Gallery
  {
  /**
   * @ORM\Id
   * @ORM\Column(type="integer")
   * @ORM\GeneratedValue(strategy="AUTO")
   * 
  
   * @var integer $gallery_id
   */
  protected $gallery_id;
  /**
  
   * @ORM\ManyToOne(targetEntity="IntA\Bundle\Entity\Listing", inversedBy="image")
     @ORM\JoinColumn(name="listing_id", referencedColumnName="listing_id")
   * @var integer 
   */
  protected $listing_gallery;
  
  /**
   * @ORM\Column(type="string", length="255")
   * 
   * @var string $name;
   */
  protected $name;
  
  /**
   * @ORM\Column(type="string", length="255")
   * 
   * @var string $description;
   */
  protected $description;
  
  /**
   * @ORM\Column(type="datetime", name="date_created")
   * 
   * @var DateTime $date_created
   */
  protected $date_created;
  
   /**
   * @ORM\Column(type="datetime", name="date_updated")
   * 
   * @var DateTime $date_updated
   */
  protected $date_updated;
  
   /**
   * @ORM\Column(type="text", length="255")
   * 
   * @var string $url;
   */    
  public $url;
  
  
  
  public $file = array();
  </code></pre>
  
  <p><strong>Gallery Controller</strong></p>
  
  <pre><code>public function uploadAction($id)
  {
      $file = new Gallery();
      $images_form    = $this-&gt;createForm(new GalleryType(), $file); 
      $form_view = $images_form-&gt;createView();
      $form_view-&gt;getChild('file')-&gt;set('full_name', 'gallery[file][]');
      $request = $this-&gt;getRequest();
      #die(var_dump($request-&gt;files));
      if ($request-&gt;getMethod() === 'POST') {
          $images_form-&gt;bindRequest($request);            
  
          $data = $images_form-&gt;getData();   
          #die(var_dump($images_form-&gt;getData()-&gt;getFile()));
  
  
  
              $em = $this-&gt;getDoctrine()-&gt;getEntityManager();
              $em-&gt;persist($file);
  
              $related_listing = $em-&gt;getRepository('IntABundle:Listing')-&gt;find($id);
              $related_listing-&gt;setImage($data);
  
              foreach($images_form-&gt;getData()-&gt;getFile() AS $singleUploadedFile){
  
              $related_listing-&gt;setImage($singleUploadedFile);
              $em-&gt;persist($related_listing);
  
              }
  
  
  
              $em-&gt;flush();
  
              $uploadedFiles = $em-&gt;getRepository('IntABundle:Listing')-&gt;findAllImagesPerListing($id);
  
  
  
              return $this-&gt;render('UploadBundle:Gallery:view_uploaded.html.twig', array(
                                      'uploadedFiles'      =&gt; $uploadedFiles,                                        
                                  ));
      }
  
      return $this-&gt;render('UploadBundle:Gallery:upload.html.twig', array(
          'images_form' =&gt; $form_view,
          'id' =&gt; $id,
      ));
  
  
  }
  </code></pre>
  
  <p>Creating a listing (with image="") works without a problem on its form. I have created a separate form in which the user can upload a "Gallery" with multiple images inside of it. The images upload great, but I can't understand how to make a connection between the existing Listing object that the user just created and the images within the Gallery object that they would like to associate with the Listing object.</p>
  
  <p>I'm not sure if my approach to the problem is right or if there exists a better approach. Right now I can create both objects, but not make the appropriate links between them. Is there example code out there to associate objects between two different forms? Perhaps I'm not looking the right places!</p>