Why my file-update Listener don't work like va

2019-06-14 12:21发布

I use a standalone version of SyliusResourceBundle (0.9) in my project. I can manage without any problem my entities: User, Group, Role and Company.

I tried using Gaufrette\Filesystem to add a logo to a company (like variant_image in sylius). And it seems that my service is not running. He tries to persist the image without it have been uploaded and I have no error message from my listener !

KNP Gaufrette Configuration in config.yml :

knp_gaufrette:
    adapters:
        project_file:
            local:
                directory:  %kernel.root_dir%/../web/media/file
                create:     true
    filesystems:
        project_file:
            adapter: project_file

ProjectCoreBundle\Resources\Config\services.yml :

#in ProjectCoreBundle\Resources\Config\services.yml
# Listener
project.listener.image_upload:
    class: %project.listener.image_upload.class%
    arguments: ['@project.image_uploader']
    tag:
        - { name: kernel.event_listener, event: project.company.pre_create, method: uploadCompanyLogo }
        - { name: kernel.event_listener, event: project.company.pre_update, method: uploadCompanyLogo }

# Other
project.image_uploader:
    class: %project.image_uploader.class%
    factory_class: Gaufrette\Filesystem
    factory_service: knp_gaufrette.filesystem_map
    factory_method: get
    arguments: [project_file]

My ImageUploadListener :

<?php

namespace Project\CoreBundle\EventListener;

use Project\CompanyBundle\Entity\CompanyInterface;
use Project\CoreBundle\Uploader\ImageUploaderInterface;
use Symfony\Component\EventDispatcher\GenericEvent;

class ImageUploadListener
{
    protected $uploader;

    public function __construct(ImageUploaderInterface $uploader)
    {
        $this->uploader = $uploader;
    }

    public function uploadCompanyLogo(GenericEvent $event)
    {
        $subject = $event->getSubject();

        if (!$subject instanceof CompanyInterface) {
            throw new UnexpectedTypeException(
                $subject,
            'Project\CompanyBundle\CompanyInterface');
        }

        $logo = $subject->getLogo();

        if ($logo->hasFile()) {
            $this->uploader->upload($logo);
        }
    }
}

When i send my form with an image, i got this message :

An exception occurred while executing 'INSERT INTO syn_image (path, createdAt, updatedAt) VALUES (?, ?, ?)' with params [null, "2014-05-23 18:13:13", "2014-05-23 18:13:13"]:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'path' cannot be null

I feel that my service does not fire, Anyone can help me with this problem ?

Thanks :)

1条回答
forever°为你锁心
2楼-- · 2019-06-14 13:20

It seems like the image uploader feature is still being developed. I've noticed that the image uploader doesn't work when there's already an image that's part of the collection as did the person who created this github issue: issue #356. There's discussion on what could be done/what tools could be used to develop a better uploader here: link.

查看更多
登录 后发表回答