简体   繁体   English

如何在sonata admin中自动将图像从父实体存储到子节点

[英]How to automatically store images from parent entity to child in sonata admin

I got stuck and I can't figure out how to solve this problem. 我卡住了,我无法弄清楚如何解决这个问题。 I am using symfony 3.4 and sonata admin . 我正在使用symfony 3.4sonata admin I have tow entities classes called Certificate and CertificateImage with the following relationships: 我有两个名为CertificateCertificateImage的实体类,它们具有以下关系:

    class Certificate 
{
        /**
     * @var Certificate
     *
     * @ORM\OneToOne(targetEntity="CMS3\CoreBundle\Entity\Certificate", inversedBy="child")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="parent_id", referencedColumnName="id", unique=true)
     * })
     */
    private $parent;


        /**
     * @var Certificate
     *
     * @ORM\OneToOne(targetEntity="CMS3\CoreBundle\Entity\Certificate", mappedBy="parent", orphanRemoval=true)
     */
    private $child;

        /**
     * @var CertificateImage[]|Collection
     *
     * @ORM\OneToMany(targetEntity="CMS3\CoreBundle\Entity\CertificateImage", mappedBy="certificate", cascade={"persist", "remove"})
     */
    private $images;

    // getters and setters 
}

class CertificateImage 
{
     /**
     * @var Media
     *
     * @ORM\ManyToOne(targetEntity="Application\Sonata\MediaBundle\Entity\Media", cascade={"persist"})
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="image_id", referencedColumnName="id")
     * })
     */
    private $image;

        /**
     * @var Certificate
     *
     * @ORM\ManyToOne(targetEntity="CMS3\CoreBundle\Entity\Certificate", inversedBy="images")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="certificate_id", referencedColumnName="id")
     * })
     */
    private $certificate;

    // getters and setters
}

My goal: How can I automatically store images from parent Certificate to child Certificate when creating parent certificate images from sonata configureFormFields method? 我的目标:当从sonata configureFormFields方法创建父证书图像时,如何自动将图像从父证书存储到子证书 I really appreciate any idea about how I can achieve this. 我真的很感激有关如何实现这一目标的任何想法。 Thanks in advance. 提前致谢。 在这里输入代码

To add an image to your child entity, you have to duplicate the TicketImage instance, because its foreign key will be the parent one. 要将图像添加到子实体,您必须复制TicketImage实例,因为其外键将是父键。 You can try to do it like this (not sure, it's difficult to figure it out without the full code) : 您可以尝试这样做(不确定,没有完整的代码很难弄明白):

public function addImage(TicketImage $image): Ticket
 {
    $this->images->add($image);
    $image->setTicket($this); // works
    foreach($this->child as $child) {
        $cloneImg = clone $image;
        $cloneImg->setId(null);
        $cloneImg->setTicket($child);
        $child->setImage($cloneImg);
    }

    return $this;
 }

But I think this way looks like a hack so a better solution could be to think again your code and maybe try to reach the parent image from the children using a getter in the children like that : 但我认为这种方式看起来像黑客,所以一个更好的解决方案可能是再次思考你的代码,并尝试使用孩子中的getter来达到孩子们的父图像:

public function getImage(): TicketImage 
 {
    return $this->parent->getImages();
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM