目前存在以下内容:
列出实体
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,
));
}
创建列表(with image =“”)在其表单上没有问题。我创建了一个单独的表单,用户可以在其中上传其中包含多个图像的“图库”。图片上传效果很好,但我无法理解如何在用户刚创建的现有Listing对象与Gallery对象中他们想要与Listing对象关联的图像之间建立连接。
我不确定我对问题的解决方法是否正确,或者是否存在更好的方法。现在我可以创建两个对象,但不能在它们之间建立适当的链接。是否有示例代码将两个不同形式之间的对象关联起来?也许我找不到合适的地方!
答案 0 :(得分: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,
));
}