简体   繁体   中英

Doctrine 2 ManyToMany and preUpdate

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();
}

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();
}

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM