与softDelete内部关系的学说

时间:2017-06-27 15:51:15

标签: php symfony doctrine-orm doctrine

大家好我关于softdelete内部关系的问题。

对于softdelete我正在使用Gedmo\SoftDeleteable这是我的实体(我已经剪了很多东西)

<?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;
    }
}

我删除了一些图像并在删除图像时在我的数据库中删除了字段deleted_at被编译,所以如果我自动创建一个带有doctrine的findAll,则记录不会返回

但如果我从我的模板中调用getImages,我会检索所有图像也删除了图像,但我不想要它。

这就是我在循环中从模板中调用getImages的方法:

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

如何使用函数getImages仅检索未删除的图像?

由于

P.S。我不想在模板中插入if condition,我想在后端解决它

0 个答案:

没有答案