Skip to content

Update file_uploads.rst #3525

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions cookbook/doctrine/file_uploads.rst
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,16 @@ Next, refactor the ``Document`` class to take advantage of these callbacks::
}
}

.. caution::

When using Doctrine EvensubScribers' preUpdate(\Doctrine\Common\Persistence\Event\PreUpdateEventArgs $args)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@juanmf Can you re-word this paragraph please?

When using a Doctrine event listener or subscriber, when you make changes to your entity, the preUpdate() callback must have an extra line of code to tell Doctrine about the change:

    public function preUpdate(PreUpdateEventArgs $args)
    {
        $entity = $args->getEntity();
        // do all the file uploading logic
        // ...
        $entity->setFilename($newFilename);
        $args->setNewValue('filename', $newFilename);
    }

For more details on preUpdate restrictions, see preUpdate_ in the in the
Doctrine Events documentation.

callback instead of lifecycle callbacks you also need to invoke, ``$args->setNewValue('path', $filename);``
as at this point changeSets are not updated and the ``path`` change just made won't have effect
in the data base record.
For full reference on preUpdate event restrictions, see `preUpdate`_ in the in the Doctrine Events documentation.

.. _`preUpdate`: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#preupdate

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move the URL link to the bottom of the document? That's where we keep them all :)

The class now does everything you need: it generates a unique filename before
persisting, moves the file after persisting, and removes the file if the
entity is ever deleted.
Expand Down