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