I'm tryimg to build an Eventlistener so that when entity is created path(url) of the images are persisted to Database. I'm using Blueimp (jQuery-File-Uploader). But i'm getting an sql exception. I'm guessing that something isn't linked to my listener.
An exception occurred while executing 'INSERT INTO Image (name, url) VALUES (?, ?)' with params ["dasdasdas", null]
My config.yml
oneup_uploader:
mappings:
gallery:
frontend: blueimp
My service.yml
services:
app.bundle.upload_listener:
class: AppBundle\Eventlistener\ImageuploadListener
arguments: ["doctrine.orm.entity_manager"]
tags:
- { name: kernel.event_listener, event: oneup_uploader.post_persist, mehtod: onPostUpload}
My image Entity
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Image
*
* @ORM\Table()
* @ORM\Entity
*/
class Image
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="url", type="string", length=255)
*/
private $url;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Image
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set url
*
* @param string $url
* @return Image
*/
public function setUrl($url)
{
$this->url = $url;
return $this;
}
/**
* Get url
*
* @return string
*/
public function getUrl()
{
return $this->url;
}
}
My imageuploadListener
namespace AppBundle\Eventlistener;
use AppBundle\Entity\Image;
use Oneup\UploaderBundle\Event\PostPersistEvent;
class ImageuploadListener
{
protected $manager;
public function __construct(\Doctrine\ORM\EntityManager $manager)
{
$this->manager = $manager;
}
public function onPostUpload(PostPersistEvent $event)
{
$file = $event->getFile();
$object = new Image();
$object->setUrl($file->getPathName());
$this->manager->persist($object);
$this->manager->flush();
}
}
I'm getting an jquery 500 error, but the images are moved to a folder. The permissions are OK(all access).
POST http://localhost:8000/_uploader/gallery/upload 500 (Internal Server Error)
EDIT: I found this in my error log:
[2015-04-03 11:23:23] event.INFO: An exception was thrown while getting the uncalled listeners (Catchable Fatal Error: Argument 1 passed to AppBundle\Eventlistener\ImageuploadListener::__construct() must be an instance of AppBundle\Eventlistener\EntityManager, array given, called in D:\x...\app\cache\dev\appDevDebugProjectContainer.php on line 359 and defined) {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\ContextErrorException(code: 0): Catchable Fatal Error: Argument 1 passed to AppBundle\\Eventlistener\\ImageuploadListener::__construct() must be an instance of AppBundle\\Eventlistener\\EntityManager, array given, called in D:\\....\\app\\cache\\dev\\appDevDebugProjectContainer.php on line 359 and defined at D:\\...\\src\\AppBundle\\Eventlistener\\ImageuploadListener.php:12)"} []
EDIT 2: I have changed my EntityManager to \Doctrine\ORM\EntityManager And now i'm getting a different error(dev.log):
request.CRITICAL: Uncaught PHP Exception Symfony\Component\Debug\Exception\ContextErrorException: "Warning: call_user_func() expects parameter 1 to be a valid callback, class 'AppBundle\Eventlistener\ImageuploadListener' does not have a method 'onOneupuploaderPostpersist'" at D:\...\vendor\symfony\symfony\src\Symfony\Component\EventDispatcher\Debug\WrappedListener.php line 61
Any Idea why? Can please someone explain to me what I'm doing wrong? Which file is calling this method onOneupuploaderPostpersist ?
in your file service.yml
mehtod: onPostUpload
is typo bug, instead of "mehtod" you should have "method"
this method is probably default method that is called on listener by OneupUploaderBundle if not specified otherwise