当我删除实体时,关系OneToOne尚未删除。 Gallery已从数据库中成功删除,但largeImage,mediumImage和smallImage没有。
我做错了什么?
删除控制器中的操作:
public function deleteAction($id)
{
$em = $this->getDoctrine()->getManager();
$offer = $em->getRepository('ToCmsBundle:Offer')->find($id);
if($offer) {
if(!$offer->getGallery()->isEmpty()) {
foreach($offer->getGallery() as $key => $image) {
$image->setOffer(null);
$offer->removeGallery($image);
//$em->remove($image);
//$em->flush();
echo $image->getPath() . '<br/>';
}
} else {
echo 'gallery is empty';
}
$offer->setLargeImage(null);
$offer->setMediumImage(null);
$offer->setSmallImage(null);
$em->remove($offer);
$em->flush();
}
return $this->redirect($this->generateUrl('to_offer_list'));
}
我的老板班:
/**
* Offer
*
* @ORM\Table(name="to_offer")
* @ORM\HasLifecycleCallbacks
* @ORM\Entity(repositoryClass="To\CmsBundle\Repository\OfferRepository")
*/
class Offer {
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* @var string
*
* @ORM\Column(name="content", type="text")
*/
private $content;
/**
* @var ArrayCollection
*
* @ORM\OneToMany(targetEntity="Image", mappedBy="offer", cascade={"all"}, orphanRemoval=true)
*/
private $gallery;
/**
* @var Image
*
* @ORM\OneToOne(targetEntity="Image", cascade={"all"}, orphanRemoval=true)
*/
public $largeImage;
/**
* @var Image
*
* @ORM\OneToOne(targetEntity="Image", cascade={"all"}, orphanRemoval=true)
*/
public $mediumImage;
/**
* @var Image
*
* @ORM\OneToOne(targetEntity="Image", cascade={"all"}, orphanRemoval=true)
*/
public $smallImage;
/**
* @var ArrayCollection
*
* @ORM\ManyToMany(targetEntity="Category", inversedBy="articles")
* @ORM\JoinTable(name="to_articles_categories")
*/
private $categories;
/**
* @var User
*
* @ORM\ManyToOne(targetEntity="User")
* @ORM\JoinColumn(name="author_id", referencedColumnName="id")
*/
private $author;
public function __construct() {
$this->gallery = new ArrayCollection();
$this->categories = new ArrayCollection();
}
/** other stuff */
}
图片类:
/**
* Image
*
* @ORM\Table(name="to_images")
* @ORM\Entity(repositoryClass="To\CmsBundle\Repository\ImageRepository")
* @ORM\HasLifecycleCallbacks
*/
class Image
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="hashName", type="string", length=255)
*/
private $hashName;
/**
* @var integer
*
* @ORM\Column(name="position", type="integer")
*/
private $position;
/**
* @var string
*
* @ORM\Column(name="path", type="string", length=255)
*/
private $path;
/**
* @var string
*
* @ORM\Column(name="thumbnail", type="string", length=255)
*/
private $thumbnail;
/**
* @var string
*
* @ORM\Column(name="originalImage", type="string", length=255)
*/
private $originalImage;
/**
* @var Offer
*
* @ORM\ManyToOne(targetEntity="Offer", inversedBy="gallery")
* @ORM\JoinColumn(name="offer_id", referencedColumnName="id", nullable=true)
*/
private $offer;
/**
* @var \DateTime
*
* @Gedmo\Timestampable(on="create")
* @ORM\Column(name="createdAt", type="datetime")
*/
private $createdAt;
/**
* @var \DateTime
*
* @Gedmo\Timestampable(on="update")
* @ORM\Column(name="updatedAt", type="datetime")
*/
private $updatedAt;
/**
* @var integer
*
* @ORM\Column(name="x", type="integer")
*/
protected $x = 0;
/**
* @var integer
*
* @ORM\Column(name="y", type="integer")
*/
protected $y = 0;
/**
* @var integer
*
* @ORM\Column(name="w", type="integer")
*/
protected $w = 0;
/**
* @var integer
*
* @ORM\Column(name="h", type="integer")
*/
protected $h = 0;
/**
* @var boolean
*
* @ORM\Column(name="defaultImage", type="boolean")
*/
private $default = 0;
public function __construct() {
$this->position = 0;
$this->default = 0;
$this->x = 0;
$this->y = 0;
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload()
{
$this->position = $this->position ?: 0;
$this->default = $this->default ?: 0;
$this->x = $this->x ?: 0;
$this->y = $this->y ?: 0;
/*if (null !== $this->getFile()) {
// do whatever you want to generate a unique name
$this->name = sha1(uniqid(mt_rand(), true));
$this->originName = $this->getFile()->getClientOriginalName();
$this->extension = $this->getFile()->guessExtension();
$this->path = $this->name.'.'.$this->extension;
}*/
}
/**
* @ORM\PreRemove()
*/
public function removeUpload()
{
echo $this->getRootPath().$this->originalImage; echo '<br/>';
echo $this->getRootPath().$this->thumbnail; echo '<br/>';
echo $this->getRootPath().$this->path; echo '<br/>';
echo '<br/><br/>';
if(file_exists($this->getRootPath().$this->originalImage))
unlink($this->getRootPath().$this->originalImage);
if(file_exists($this->getRootPath().$this->thumbnail))
unlink($this->getRootPath().$this->thumbnail);
if(file_exists($this->getRootPath().$this->path))
unlink($this->getRootPath().$this->path);
}
/** other stuff */
}
答案 0 :(得分:2)
您的实体互相引用,因此您应尝试删除这些:
$offer->setLargeImage(null);
$offer->setMediumImage(null);
$offer->setSmallImage(null);
并在flush()
之前和之后拨打$em->remove($offer);
一次。
foreach ($offer->getGallery() as $image) {
$offer->removeGallery($image);
}
$em->flush();
$em->remove($offer);
$em->flush();
只有当所有图像都是“私有”时,这才会起作用,这意味着它们不会在此实体或其他实体中多次引用。
答案 1 :(得分:0)
$offer->setLargeImage(null);
$offer->setMediumImage(null);
$offer->setSmallImage(null);
当你删除它时,你的“$ offer”不再有图像。
<强>更新强>
当您执行'find()'时,由于延迟加载,您没有图像对象,而是代理。如果您需要包含图像对象的商品,请在图像上使用leftJoin创建查询以获取商品AND图像。然后,我认为你不会有问题。
(请查看本教程的结尾,以了解对象上的leftjoin示例:http://symfony.com/fr/doc/current/cookbook/security/entity_provider.html#gerer-les-roles-via-la-base-de-donnees)