Roman Newaza
Roman Newaza

Reputation: 11690

Trigger Doctrine Event

I have an event listener for preUpdate Doctrine Event which does the job just fine, but if request data is empty except image_data, it is not triggered. And it's logically correct, because there is no image_data column in an Entity, thus it doesn't see a field to change. The idea is to process image_data array, then do an image upload and finally store filename to image ORM column which works if one of the fields present in request. Let me show my code:

Controller

public function patch($id, Request $request)
{
    $data = $request->request->all();
    $company = $this->repo->find($id);
    $form = $this->createForm(CompanyType::class, $company);
    $form->submit($data, false);
    if (false === $form->isValid()) {
        // error
    }
    $this->em->flush();
    // success
}

Form

// ...
->add('image_data', Types\TextType::class)
->add('image', Types\TextType::class)
// ...

Config

# ...
- { name: doctrine.event_listener, event: preUpdate }

Entity

trait UploadableTrait
{
    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $image;

    /**
     * @Exclude()
     */
    private $image_data = [];

How I solved it so far - in BeforeActionSubscriber I set image property to 1 therefore preUpdate is fired, upload is handled and real image filename is stored in result. I believe there is smarter way of doing it. Formerly, I used single image parameter for input/output and it worked for preUpdate because there is such ORM column, however I didn't like this approach because incoming image data is an array (image_name, image_body, content_type and image_size), while output data type is string (filename) and I decided to separate it (image_data for POST|PATCH|PUT and image is just result filename). How may I trigger preUpdate? :)

Upvotes: 1

Views: 327

Answers (1)

Iwan Wijaya
Iwan Wijaya

Reputation: 2157

You can use an updatedAt field like this: https://github.com/dustin10/VichUploaderBundle/blob/master/Resources/doc/known_issues.md#the-file-is-not-updated-if-there-are-not-other-changes-in-the-entity

class MyEntitty
{
    // ...

    /**
     * @ORM\Column(type="datetime")
     *
     * @var \DateTime|null
     */
    private $updatedAt;

    // ...

    public function setSomething($something): void
    {
        $this->something= $something;
        $this->updatedAt = new \DateTime('now');
    }
}

Upvotes: 1

Related Questions