[FLOW3-general] Doctrine's removeElement() deletes objects without repository update
Martin Lipp
martin.lipp at gmail.com
Tue Feb 14 20:16:55 CET 2012
Hi,
you're right. It is only happening, if the property is a OneToMany
relation. I created a new issue with some more details on forge:
http://forge.typo3.org/issues/33921
Martin
On 14.02.12 09:10, "Christian Müller (FLOW3 Team)" wrote:
> Hi Martin,
>
> normally persisting a change *should* only happen if you do update() or
> delete() so I expect that you need to update($album) after removing the
> image, anyway that sounds like a bug to me. Any further hints in a bug
> report on forge would be great! Thank you.
>
> About the difference... The album->images is a OneToMany relation and as
> you said you only have an album repository, so if you remove an image
> from an album it won't have another album, so you couldn't retrieve
> anymore without the repository, that is why it is deleted. A cascading
> is implicit here. As for $image->removePerson I guess there was maybe
> another relation to the same person or (I would need to check that) the
> cascading is not implicit for ManyToMany relations. So that behaviour
> should be fine.
>
>
> Cheers,
> Christian
>
>
> On 13/02/12 22:04, Martin Lipp wrote:
>> Is it intended that the Doctrine ArrayCollection removeElement($object)
>> function forces the deletion of $object out of the database? I always
>> was of the opinion that only repository functions like update() or
>> delete() are able to do persistence stuff.
>> After testing a little bit it seems not to happen with every of my
>> models, but I'm not able to see any difference between the models.
>>
>>
>> Models:
>>
>> /**
>> * A Album
>> *
>> * @FLOW3\Scope("prototype")
>> * @FLOW3\Entity
>> */
>> class Album {
>>
>> /**
>> * The images
>> * @var \Doctrine\Common\Collections\Collection<...\Domain\Model\Image>
>> * @ORM\OneToMany(mappedBy="album")
>> * @ORM\OrderBy({"fileName" = "ASC"})
>> */
>> protected $images;
>> ...
>> /**
>> * Removes an image from this album
>> *
>> * @param ...\Domain\Model\Image $image
>> * @return void
>> */
>> public function removeImage(...\Domain\Model\Image $image) {
>> $this->images->removeElement($image);
>> }
>>
>> ...
>>
>> /**
>> * A Image
>> *
>> * @FLOW3\Scope("prototype")
>> * @FLOW3\Entity
>> */
>> class Image {
>> /**
>> * The album
>> * @var ...\Domain\Model\Album
>> * @ORM\ManyToOne(inversedBy="images")
>> */
>> protected $album;
>>
>> /**
>> * The persons
>> * @var \Doctrine\Common\Collections\Collection<...\Domain\Model\Person>
>> * @ORM\ManyToMany(inversedBy="images")
>> */
>> protected $persons;
>>
>> ...
>>
>> /**
>> * A Person
>> *
>> * @FLOW3\Scope("prototype")
>> * @FLOW3\Entity
>> */
>> class Person {
>>
>> /**
>> * The images tagged with this person
>> *
>> * @var
>> \Doctrine\Common\Collections\Collection<\LA\SCGussendorf\Domain\Model\Image>
>>
>>
>> * @ORM\ManyToMany(mappedBy="persons")
>> */
>> protected $images;
>>
>> ...
>>
>>
>> Only the AlbumRepository exists. A simple $album->removeImage($image)
>> (without $this->albumRepository->update($album) ) deletes the image, the
>> ManyToMany relation and even the connected Persons from the database.
>> $image->removePerson($person) for instance only deletes the ManyToMany
>> entry in the join table (and only after the update() command).
>>
>> Martin
>
More information about the FLOW3-general
mailing list