Skip to content

Commit

Permalink
Use preFlush event introduced in Doctrine 2.2 and minior refactoring (c…
Browse files Browse the repository at this point in the history
  • Loading branch information
Koc committed Sep 2, 2016
1 parent 0ecb31a commit dd0d5fb
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 174 deletions.
4 changes: 2 additions & 2 deletions EventListener/Doctrine/BaseListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function __construct($mapping, AdapterInterface $adapter, MetadataReader
/**
* Checks if the given object is uploadable using the current mapping.
*
* @param mixed $object The object to test.
* @param object $object The object to test.
*
* @return bool
*/
Expand All @@ -67,7 +67,7 @@ protected function isUploadable($object)
/**
* Returns a list of uploadable fields for the given object and mapping.
*
* @param mixed $object The object to use.
* @param object $object The object to use.
*
* @return array<string> A list of field names.
*/
Expand Down
15 changes: 6 additions & 9 deletions EventListener/Doctrine/CleanListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,30 @@
namespace Vich\UploaderBundle\EventListener\Doctrine;

use Doctrine\Common\EventArgs;
use Doctrine\ORM\Events;

/**
* CleanListener
*
* Listen to the update event to delete old files accordingly.
*
* @author Kévin Gomez <[email protected]>
* @author Konstantin Myakshin <[email protected]>
*/
class CleanListener extends BaseListener
{
/**
* The events the listener is subscribed to.
*
* @return array The array of events.
* {@inheritdoc}
*/
public function getSubscribedEvents()
{
return array(
'preUpdate',
);
return array(Events::preFlush);
}

/**
* @param EventArgs $event The event.
* {@inheritdoc}
*/
public function preUpdate(EventArgs $event)
public function preFlush(EventArgs $event)
{
$object = $this->adapter->getObjectFromArgs($event);

Expand All @@ -38,7 +36,6 @@ public function preUpdate(EventArgs $event)

foreach ($this->getUploadableFields($object) as $field) {
$this->handler->clean($object, $field);
$this->adapter->recomputeChangeSet($event);
}
}
}
11 changes: 4 additions & 7 deletions EventListener/Doctrine/InjectListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Vich\UploaderBundle\EventListener\Doctrine;

use Doctrine\Common\EventArgs;
use Doctrine\DBAL\Events;

/**
* InjectListener
Expand All @@ -14,19 +15,15 @@
class InjectListener extends BaseListener
{
/**
* The events the listener is subscribed to.
*
* @return array The array of events.
* {@inheritdoc}
*/
public function getSubscribedEvents()
{
return array(
'postLoad',
);
return array(Events::postLoad);
}

/**
* @param EventArgs $event The event.
* {@inheritdoc}
*/
public function postLoad(EventArgs $event)
{
Expand Down
25 changes: 11 additions & 14 deletions EventListener/Doctrine/RemoveListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Doctrine\Common\EventArgs;
use Doctrine\Common\Persistence\Proxy;
use Doctrine\ORM\Events;

/**
* RemoveListener
Expand All @@ -15,34 +16,30 @@
class RemoveListener extends BaseListener
{
/**
* The events the listener is subscribed to.
*
* @return array The array of events.
* {@inheritdoc}
*/
public function getSubscribedEvents()
{
return array(
'preRemove',
'postRemove',
Events::preRemove,
Events::postRemove,
);
}

/**
* Ensures a proxy will be usable in the postRemove.
*
* @param EventArgs $event The event.
* {@inheritdoc}
*/
public function preRemove(EventArgs $event)
{
$object = $this->adapter->getObjectFromArgs($event);

if ($this->isUploadable($object) && $object instanceof Proxy) {
$object->__load();
}
$object = $this->adapter->getObjectFromArgs($event);
// Ensures a proxy will be usable in the postRemove.
if ($this->isUploadable($object) && $object instanceof Proxy) {
$object->__load();
}
}

/**
* @param EventArgs $event The event.
* {@inheritdoc}
*/
public function postRemove(EventArgs $event)
{
Expand Down
33 changes: 6 additions & 27 deletions EventListener/Doctrine/UploadListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,30 @@
namespace Vich\UploaderBundle\EventListener\Doctrine;

use Doctrine\Common\EventArgs;
use Doctrine\ORM\Events;

/**
* UploadListener
*
* Handles file uploads.
*
* @author Kévin Gomez <[email protected]>
* @author Konstantin Myakshin <[email protected]>
*/
class UploadListener extends BaseListener
{
/**
* The events the listener is subscribed to.
*
* @return array The array of events.
* {@inheritdoc}
*/
public function getSubscribedEvents()
{
return array(
'prePersist',
'preUpdate',
);
return array(Events::preFlush);
}

/**
* @param EventArgs $event The event.
* {@inheritdoc}
*/
public function prePersist(EventArgs $event)
public function preFlush(EventArgs $event)
{
$object = $this->adapter->getObjectFromArgs($event);

Expand All @@ -41,22 +38,4 @@ public function prePersist(EventArgs $event)
$this->handler->upload($object, $field);
}
}

/**
* @param EventArgs $event The event.
*/
public function preUpdate(EventArgs $event)
{
$object = $this->adapter->getObjectFromArgs($event);

if (!$this->isUploadable($object)) {
return;
}

foreach ($this->getUploadableFields($object) as $field) {
$this->handler->upload($object, $field);
}

$this->adapter->recomputeChangeSet($event);
}
}
39 changes: 0 additions & 39 deletions Resources/doc/known_issues.md
Original file line number Diff line number Diff line change
@@ -1,45 +1,6 @@
Known issues
============

## The file is not updated if there are not other changes in the entity

As the bundle is listening to Doctrine `prePersist` and `preUpdate` events, which are not fired
when there is no change on field mapped by Doctrine, the file upload is not handled if the image field
is the only updated.

A workaround to solve this issue is to manually generate a change:

```php
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class Product
{
// ...

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

// ...

public function setImage(File $image = null)
{
$this->image = $image;

// Only change the updated af if the file is really uploaded to avoid database updates.
// This is needed when the file should be set when loading the entity.
if ($this->image instanceof UploadedFile) {
$this->updatedAt = new \DateTime('now');
}
}
}
```
See issue [GH-123](https://github.com/dustin10/VichUploaderBundle/issues/123)

## Catchable Fatal Error

When you get the following error:`Catchable Fatal Error: ... must be an instance of Symfony\Component\HttpFoundation\File\UploadedFile, string given, ...`
Expand Down
20 changes: 6 additions & 14 deletions Tests/EventListener/Doctrine/CleanListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Vich\UploaderBundle\Tests\EventListener\Doctrine;

use Doctrine\ORM\Events;
use Vich\UploaderBundle\EventListener\Doctrine\CleanListener;

/**
Expand All @@ -28,11 +29,11 @@ public function testGetSubscribedEvents()
{
$events = $this->listener->getSubscribedEvents();

$this->assertSame(array('preUpdate'), $events);
$this->assertSame(array(Events::preFlush), $events);
}

/**
* Test the preUpdate method.
* Test the preFlush method.
*/
public function testPreUpdate()
{
Expand All @@ -55,16 +56,11 @@ public function testPreUpdate()
->method('clean')
->with($this->object, 'field_name');

$this->adapter
->expects($this->once())
->method('recomputeChangeSet')
->with($this->event);

$this->listener->preUpdate($this->event);
$this->listener->preFlush($this->event);
}

/**
* Test that preUpdate skips non uploadable entity.
* Test that preFlush skips non uploadable entity.
*/
public function testPreUpdateSkipsNonUploadable()
{
Expand All @@ -78,10 +74,6 @@ public function testPreUpdateSkipsNonUploadable()
->expects($this->never())
->method('clean');

$this->adapter
->expects($this->never())
->method('recomputeChangeSet');

$this->listener->preUpdate($this->event);
$this->listener->preFlush($this->event);
}
}
3 changes: 2 additions & 1 deletion Tests/EventListener/Doctrine/InjectListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Vich\UploaderBundle\Tests\EventListener\Doctrine;

use Doctrine\ORM\Events;
use Vich\UploaderBundle\EventListener\Doctrine\InjectListener;

/**
Expand All @@ -28,7 +29,7 @@ public function testGetSubscribedEvents()
{
$events = $this->listener->getSubscribedEvents();

$this->assertSame(array('postLoad'), $events);
$this->assertSame(array(Events::postLoad), $events);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion Tests/EventListener/Doctrine/RemoveListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Vich\UploaderBundle\Tests\EventListener\Doctrine;

use Doctrine\ORM\Events;
use Vich\UploaderBundle\EventListener\Doctrine\RemoveListener;

/**
Expand All @@ -28,7 +29,7 @@ public function testGetSubscribedEvents()
{
$events = $this->listener->getSubscribedEvents();

$this->assertSame(array('preRemove', 'postRemove'), $events);
$this->assertSame(array(Events::preRemove, Events::postRemove), $events);
}

public function testPreRemove()
Expand Down
Loading

0 comments on commit dd0d5fb

Please sign in to comment.