当前存在以下内容:
上市实体
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,
));
}
创建列表(使用 image="")在其表单上没有问题。我创建了一个单独的表单,用户可以在其中上传包含多个图像的“图库”。图像上传得很好,但我不明白如何在用户刚刚创建的现有列表对象和他们希望与列表对象关联的图库对象中的图像之间建立连接。
我不确定我解决问题的方法是否正确,或者是否存在更好的方法。现在我可以创建这两个对象,但不能在它们之间建立适当的链接。是否有示例代码可以在两种不同形式之间关联对象?也许我没有找对地方!