1
votes

I'm using Symfony2.5 and Doctrine 2.4.2. I have two entities, PageImages which extends Page

Page:

 /**
 * @ORM\ManyToMany(targetEntity="PageImage",cascade={"persist"})
 */
private $images;

Using form collection to create add images to the Page entity

When i create a new entry for PageImage it works just fine, PrePersist,PreUpdate,PostPersist,PostUpdate all run,

But when I try to change the image for an existing PageImage the event doesn't fire and doesn't upload new images.

Update: It doesn’t updating because the $file field is just a virtual and doctrine doesn’t see new data, and events aren't fired, so the easiest solution to do:

  public function setFile(UploadedFile $file = null)
{
    $this->temp = $this->getPath();
    $this->file = $file;
    $this->preUpload();
}
2
If you've found the answer yourself, great! Answering your own question is allowed and is encouraged if other people's answers didn't work for you. Please post it as the answer, below, and not as part of the question. - serakfalcon

2 Answers

1
votes

I found a solution, maybe its not very beautiful and someone can suggest any other way to solve the problem.

I've figured out, what’s the problem, because the $file is virtual field doctrine thinks there’s no new data in the entity and no need to persist or update it and the events doesn’t fired so I need to run it myself ot setFile():

public function setFile(UploadedFile $file = null)
{
    $this->temp = $this->getPath();
    $this->file = $file;
    $this->preUpload();
}
-2
votes

In your Edit action you will have to manually exploit the 'upload' method to current image like this

      $currentImage->upload();
      $entityManager->persist($currentImage);  
      $entityManager->flush();