Sylius自定义模型图片上传失败:监听事件未触发致path字段为空
Hey there, let's break down why those sylius.designer.pre_create and sylius.designer.pre_update events aren't firing (leading to your path field being null on persist). Here are the most common issues to check step by step:
1. Incorrect Event Naming
Sylius resource events follow a strict pattern: sylius.{resource_name}.pre_create where {resource_name} matches the short name of your registered resource.
- Double-check your resource configuration in
config/packages/sylius_resource.yaml. If your resource is defined as:
The correct event names should besylius_resource: resources: app.designer: classes: model: App\Entity\Designersylius.designer.pre_createandsylius.designer.pre_update(note: no pluralization unless your resource name uses it). - Verify the event exists by running:
If no listeners show up, your event name is likely wrong.bin/console debug:event-dispatcher sylius.designer.pre_create
2. Misconfigured Listener Service
Your listener might not be registered correctly with the event dispatcher:
- Check your
config/services.yamlto ensure the listener is tagged properly:services: App\EventListener\DesignerImageUploadListener: tags: - { name: kernel.event_listener, event: sylius.designer.pre_create, method: uploadImage } - { name: kernel.event_listener, event: sylius.designer.pre_update, method: uploadImage }- Confirm the
eventvalues match exactly what you're expecting. - Ensure the
methodname matches the method in your listener class that handles the upload.
- Confirm the
- Validate the service exists with:
If it doesn't appear, make sure your listeners are auto-discovered (e.g.,bin/console debug:container App\EventListener\DesignerImageUploadListenerApp\EventListener\: resource: '../src/EventListener/'is present in your services config).
3. Custom Model Doesn't Implement ResourceInterface
Sylius only fires resource events for entities that implement Sylius\Component\Resource\Model\ResourceInterface (or extend the abstract Resource class):
- Update your
Designerentity to either:
Or implement the interface directly (and add theuse Sylius\Component\Resource\Model\Resource; class Designer extends Resource { // Your entity fields and logic }getId()method):use Sylius\Component\Resource\Model\ResourceInterface; class Designer implements ResourceInterface { public function getId(): ?int { return $this->id; } // Rest of your entity code }
4. Custom Controller Isn't Using Sylius Resource Logic
If you built a custom controller instead of extending Sylius' ResourceController, it won't automatically fire the pre-create/pre-update events:
- Ensure your create/update actions extend or use the
ResourceControllermethods. For example, a basic create action should look like:
If you have a custom action, make sure you explicitly dispatch the events or calluse Sylius\Bundle\ResourceBundle\Controller\ResourceController; class DesignerController extends ResourceController { // No need to override createAction unless you need custom logic // The parent class handles event dispatching automatically }$this->create()/$this->update()which handle event firing.
5. Event Propagation Is Stopped
Another listener might be stopping event propagation before your listener gets triggered:
- Check other event listeners/subscribers for your resource events. Look for code like:
If found, adjust that listener to not stop propagation, or reorder your listener to run earlier (add a$event->stopPropagation();prioritytag attribute with a higher number, e.g.,priority: 100).
Once you fix the event firing issue, your image upload logic should populate the path field correctly, resolving the SQL error.
内容的提问来源于stack exchange,提问作者mangelsnc

