1

我正在尝试构建一个事件监听器,以便在创建实体时将图像的路径(url)持久化到数据库中。我正在使用 Blueimp (jQuery-File-Uploader)。但我得到了一个 sql 异常。我猜有些东西与我的听众无关。

An exception occurred while executing 'INSERT INTO Image (name, url) VALUES (?, ?)' with params ["dasdasdas", null]

我的 config.yml

oneup_uploader:
mappings:
    gallery:
        frontend: blueimp

我的服务.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}

我的图像实体

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;
}
}

我的 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();
}

}

我收到 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 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)"} []

编辑 2:我已将 EntityManager 更改为 \Doctrine\ORM\EntityManager 现在我得到一个不同的错误(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

任何想法为什么?请有人向我解释我做错了什么?哪个文件在调用此方法 onOneuploaderPostpersist ?

4

1 回答 1

1

在你的文件service.yml

mehtod: onPostUpload

是拼写错误,而不是“ mehtod ”你应该有“方法

如果未另行指定,此方法可能是 OneuploaderBundle 在侦听器上调用的默认方法

于 2015-04-04T09:40:37.540 回答