How to add multiple images to an entity?
Prerequisites
Step 1: Create the Image Entity
<?php
// src/Entity/Shipping/ShippingMethodImage.php
namespace App\Entity\Shipping;
use Sylius\Component\Core\Model\Image;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
#[ORM\Table(name: 'app_shipping_method_image')]
class ShippingMethodImage extends Image
{
#[ORM\ManyToOne(
targetEntity: ShippingMethod::class,
inversedBy: 'images'
)]
#[ORM\JoinColumn(
name: 'owner_id',
referencedColumnName: 'id',
nullable: false,
onDelete: 'CASCADE'
)]
protected $owner = null;
}Step 2: Extend the Owner Entity
Step 3: Configure Resources
Step 4: Create the Image Form Type
Step 5: Extend the Form for Shipping Method
Step 6: Enable Image Upload via Listener
Step 7: (Optional) Add Validation Constraints
Step 8: Customize the Shipping Method twig hooks

Step 9: Generate and Run Migrations
Step 10: Result

Last updated
Was this helpful?
