douban2014 2017-06-27 15:51
浏览 21

与softDelete内部关系的学说

Hi all I have a problem about softdelete inside relation.

For the softdelete I'm using Gedmo\SoftDeleteable and this is my entity (I have cut a lot of things)

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use JMS\Serializer\Annotation as JMS;


/**
 * Item
 *
 * @ORM\Table(name="item")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\ItemRepository")
 * @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false)
 */
class Item implements EntityInterface
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

   /**
     * One Item has Many Images.
     * @ORM\OneToMany(targetEntity="Image", mappedBy="item", cascade="remove")
     * @MaxDepth(1)
     * @JMS\Groups({"images"})
     */
    private $images;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="deleted_at", type="datetime", nullable=true)
     */
    protected $deletedAt;

    public function getId()
    {
        return $this->id;
    }

    public function __construct()
    {
        $this->images = new \Doctrine\Common\Collections\ArrayCollection();
    }

    public function addImage(\AppBundle\Entity\Image $image)
    {
        $this->images[] = $image;

        return $this;
    }

    public function removeImage(\AppBundle\Entity\Image $image)
    {
        $this->images->removeElement($image);
    }

    public function getImages()
    {
        return $this->images;
    }

    public function getImagePreview()
    {
        foreach($this->getImages() as $image){
            if ($image->getPreview()) {
                return substr($image->getPath(), strpos($image->getPath(), "/images/") + 8);
            }
        }

        return '';
    }

    public function setDeletedAt($deletedAt)
    {
        $this->deletedAt = $deletedAt;

        return $this;
    }


    public function getDeletedAt()
    {
        return $this->deletedAt;
    }
}

I have some images deleted and inside my database when an image is deleted field deleted_at are compiled so if I make a findAll with doctrine automatically that records not return

But if I call getImages from my template I retrieve all images also deleted images, but I don't want It.

This is how I call getImages from my template inside a loop:

foreach($lot->getImages() as $image) { 
    //code
}

how can I use function getImages to retrieve only images not deleted?

Thanks

P.S. I don't want to insert an if condition inside the template, I wanna solve It in the backend

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭
    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么