2012-06-12 37 views
1

電流以下的存在:主義/ Symfony2的關係超過2種形式

清單實體

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; 

標籤實體(存在,但代碼這個問題不相關)

圖片實體

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(); 

庫控制器

public function uploadAction($id) 
{ 
    $file = new Gallery(); 
    $images_form = $this->createForm(new GalleryType(), $file); 
    $form_view = $images_form->createView(); 
    $form_view->getChild('file')->set('full_name', 'gallery[file][]'); 
    $request = $this->getRequest(); 
    #die(var_dump($request->files)); 
    if ($request->getMethod() === 'POST') { 
     $images_form->bindRequest($request);    

     $data = $images_form->getData(); 
     #die(var_dump($images_form->getData()->getFile())); 



      $em = $this->getDoctrine()->getEntityManager(); 
      $em->persist($file); 

      $related_listing = $em->getRepository('IntABundle:Listing')->find($id); 
      $related_listing->setImage($data); 

      foreach($images_form->getData()->getFile() AS $singleUploadedFile){ 

      $related_listing->setImage($singleUploadedFile); 
      $em->persist($related_listing); 

      } 



      $em->flush(); 

      $uploadedFiles = $em->getRepository('IntABundle:Listing')->findAllImagesPerListing($id); 



      return $this->render('UploadBundle:Gallery:view_uploaded.html.twig', array(
            'uploadedFiles'  => $uploadedFiles,           
           )); 
    } 

    return $this->render('UploadBundle:Gallery:upload.html.twig', array(
     'images_form' => $form_view, 
     'id' => $id, 
    )); 


} 

創建的列表(含圖片= 「」),而其形式有問題的作品。我創建了一個單獨的窗體,用戶可以在窗體中上傳一個包含多個圖像的「圖庫」。圖片上傳效果很好,但我無法理解如何在用戶剛剛創建的現有列表對象與Gallery對象中想要與該對象關聯的圖像之間建立連接。

我不確定我對問題的處理方法是否正確,或者是否存在更好的方法。現在我可以創建兩個對象,但不能在它們之間建立適當的鏈接。是否有示例代碼在兩個不同的表單之間關聯對象?也許我不看正確的地方!

回答

1

結帳在教義關係的擁有和翻轉邊: http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/association-mapping.html

至於我可以看到你需要將設置從畫廊的關係,許多方面應該是所有者。

$gallery->setListing($listing); 

而不是

$listing->setImage($gallery) 

因爲當你嘗試並保存有關係的另一方不會存儲的關係。

這會對你的行動看起來像:

public function uploadAction($id) 
{ 
    $file = new Gallery(); 
    $images_form = $this->createForm(new GalleryType(), $file); 
    $form_view = $images_form->createView(); 
    $form_view->getChild('file')->set('full_name', 'gallery[file][]'); 
    $request = $this->getRequest(); 
    if ($request->getMethod() === 'POST') { 
     $images_form->bindRequest($request); 

     $em = $this->getDoctrine()->getEntityManager(); 

     $related_listing = $em->getRepository('IntABundle:Listing')->find($id); 
     $file->setListingGallery($related_listing); 
     $em->persist($file); 
     $em->flush(); 

     $uploadedFiles = $em->getRepository('IntABundle:Listing') 
      ->findAllImagesPerListing($id); 



     return $this->render('UploadBundle:Gallery:view_uploaded.html.twig', array(
       'uploadedFiles' => $uploadedFiles, 
      )); 
    } 

    return $this->render('UploadBundle:Gallery:upload.html.twig', array(
      'images_form' => $form_view, 
      'id' => $id, 
     )); 
} 
+0

我現在明白它背後的邏輯,特別是學習多一點從Dotrine文檔後。所以在這種情況下,Gallery實體是所有者,因爲它應該是多方面的。擁有一個擁有該列表的圖庫似乎有點不直觀。 – Adam

+0

當你從邏輯上思考它時,它的奇怪之處在於,文檔確實說ORM的關係可能與你的領域模型不同。很高興幫助,您是否設法使關係正常工作並正確保存 – Luke

+0

正在編輯代碼以集成解決方案。另一個快速的問題,如果你可以幫助:Gallery實體中的setListingGallery應該帶一個Listing類型的參數?我只看到了setter的許多方面,其中類型只是ArrayCollection – Adam