dsoy71058 2019-05-07 12:41
浏览 34

如何在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. enter code here

  • 写回答

1条回答 默认 最新

  • dongwuwei0718 2019-05-07 16:55
    关注

    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();
     }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看