2实体之间的教义/ Symfony2的关系超过2种形式 [英] Doctrine/Symfony2 Relationship Between 2 Entities over 2 Forms

查看:148
本文介绍了2实体之间的教义/ 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; 

标签实体(存在,但是code这个问题不相关)

Tag Entity (Exists, but code not relevant for this problem)

图库实体

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


}

创建的列表(含图片=)作品,未经其表单上的问题。我创建了一个单独的表格中,用户可以上传一个画廊与它内部的多个图像。图像上传伟大的,但我不明白如何让用户刚刚创建的现有上市对象和库对象中的图像之间的连接,他们想上市对象关联。

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.

我不知道如果我的解决问题的方法是正确的,或者如果存在一个更好的办法。现在,我可以创建两个对象,但不能使它们之间适当的链接。是否有例如code那里以两种不同形式的对象相关联?也许我不是在寻找正确的地方!

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!

推荐答案

在结账人际关系学说的拥有和逆双方:
<一href=\"http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/association-mapping.html\" rel=\"nofollow\">http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/association-mapping.html

Checkout the owning and inverse sides in doctrine relationships: http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/association-mapping.html

据我可以看到你需要将设置从画廊的关系,许多方面应该是所有者。

As far as I can see you need to be setting the relationship from the gallery as the many side should be the owner.

$gallery->setListing($listing);

而不是

$listing->setImage($gallery)

由于当您尝试关系的反向端将不会存储的关系并保存它。

Because the inverse side of a relationship will not store the relation when you try and save it.

这会对你的行动看起来像:

That would have your action looking like:

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,
        ));
}

这篇关于2实体之间的教义/ Symfony2的关系超过2种形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆