1

I am trying to remove galleryhasmedia from gallery.

However gallery entity doesn't have removegalleryhasmedia or something.

so I did a clumsy way, but it doesnt work.

$em = $this->getDoctrine()->getManager();

$firstGhmArray = $gallery->getGalleryHasMedias();
echo count($gallery->getGalleryHasMedias()) // before count
$afterGhmArray = array();
foreach ($firstGhmArray as $ghm){

        if ($ghm->getId() == $id){ // id is the target id to delete
                //delete    
        }
        else {
            array_push($afterGhmArray , $ghm);
        }
        $gallery->setGalleryHasMedias($afterGhmArray);
    }
echo count($gallery->getGalleryHasMedias()) // after count
$em->persist($gallery);
$em->flush();

I think if galleryHasMedias are normal array collection.

I can delete the element with this procedure.

I need to do something more for galleryhasmedia??

4

1 回答 1

1

您可以覆盖 Gallery 实体并将此功能添加到其中:

    public function clearGalleryHasMedias()
    {
        $this->galleryHasMedias->clear();
    }

galleryHasMedias 字段是一个 ArrayCollection,可以使用 clear 方法清除。奇怪的是,设置一个空数组并不能清除工作,但我想我的解决方案值得一试。

于 2015-04-29T08:46:50.783 回答