简体   繁体   中英

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 . I have tow entities classes called Certificate and CertificateImage with the following relationships:

    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? 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. 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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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