我正在尝试构建一个Eventlistener,以便在创建实体时将图像的路径(url)持久化到数据库。我使用的是Blueimp(jQuery File Uploader)。但是我得到了一个sql异常。我猜有些东西和我的听众没有联系。
An exception occurred while executing 'INSERT INTO Image (name, url) VALUES (?, ?)' with params ["dasdasdas", null]
我的配置yml
oneup_uploader:
mappings:
gallery:
frontend: blueimp
我的服务.yml
services:
app.bundle.upload_listener:
class: AppBundleEventlistenerImageuploadListener
arguments: ["doctrine.orm.entity_manager"]
tags:
- { name: kernel.event_listener, event: oneup_uploader.post_persist, mehtod: onPostUpload}
我的图像实体
namespace AppBundleEntity;
use DoctrineORMMapping as ORM;
/**
* Image
*
* @ORMTable()
* @ORMEntity
*/
class Image
{
/**
* @var integer
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORMColumn(name="name", type="string", length=255)
*/
private $name;
/**
* @var string
*
* @ORMColumn(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;
}
}
我的imageuploadListener
namespace AppBundleEventlistener;
use AppBundleEntityImage;
use OneupUploaderBundleEventPostPersistEvent;
class ImageuploadListener
{
protected $manager;
public function __construct(DoctrineORMEntityManager $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();
}
}
我收到一个jquery 500错误,但图像被移到了一个文件夹中。权限正常(所有访问)。
POST http://localhost:8000/_uploader/gallery/upload 500 (Internal Server Error)
编辑:我在错误日志中发现了这个:
[2015-04-03 11:23:23] event.INFO: An exception was thrown while getting the uncalled listeners (Catchable Fatal Error: Argument 1 passed to AppBundleEventlistenerImageuploadListener::__construct() must be an instance of AppBundleEventlistenerEntityManager, array given, called in D:x...appcachedevappDevDebugProjectContainer.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)"} []
第2版:我已经将EntityManager更改为\Doctrine\ORM\EntityManager,现在我收到了一个不同的错误(dev.log):
request.CRITICAL: Uncaught PHP Exception SymfonyComponentDebugExceptionContextErrorException: "Warning: call_user_func() expects parameter 1 to be a valid callback, class 'AppBundleEventlistenerImageuploadListener' does not have a method 'onOneupuploaderPostpersist'" at D:...vendorsymfonysymfonysrcSymfonyComponentEventDispatcherDebugWrappedListener.php line 61
知道为什么吗?能请谁向我解释一下我做错了什么吗?哪个文件正在OneupuploaderPostpersistent上调用此方法?
在文件service.yml中
mehtod: onPostUpload
是打字错误,而不是"mehtod",你应该有"方法"
该方法可能是OneupUploaderBundle在侦听器上调用的默认方法,如果未另行指定