Yassine Belkedah
Yassine Belkedah

Reputation: 1

Issue with Overriding LineItem in Sylius Refund Plugin

Context:

I want to extend the LineItem entity in Sylius Refund Plugin to add a code field.
I created a custom entity:

namespace App\Entity\SyliusRefundPlugin;

use Doctrine\ORM\Mapping as ORM;
use Sylius\RefundPlugin\Entity\LineItem as BaseLineItem;

/**
 * @ORM\Entity
 * @ORM\Table(name="sylius_refund_line_item")
 */
#[ORM\Entity]
#[ORM\Table(name: 'sylius_refund_line_item')]
class LineItem extends BaseLineItem implements LineItemInterface
{
    #[ORM\Column(type: 'string', length: 255, nullable: true)]
    private ?string $code = null;

    public function setCode(?string $code): void
    {
        $this->code = $code;
    }

    public function getCode(): ?string
    {
        return $this->code;
    }
}

I also created this interface:

<?php

declare(strict_types=1);

namespace App\Entity\SyliusRefundPlugin;

use Sylius\RefundPlugin\Entity\LineItemInterface as BaseLineItemInterface;

interface LineItemInterface extends BaseLineItemInterface
{
    public function getCode(): ?string;
    public function setCode(?string $code): void;
}

Configuration: I used resolve_target_entities in doctrine.yaml:

doctrine:
    orm:
        resolve_target_entities:
            Sylius\RefundPlugin\Entity\LineItem: App\Entity\SyliusRefundPlugin\LineItem

I also added this in services.yaml:

Sylius\RefundPlugin\Entity\LineItem:
    class: 'App\Entity\SyliusRefundPlugin\LineItem'

Symptoms:

  1. Doctrine recognizes the custom entity:

    • bin/console doctrine:mapping:info shows App\Entity\SyliusRefundPlugin\LineItem
    • bin/console doctrine:schema:validate shows correct mapping and schema sync.
  2. When running a migration with:

bin/console doctrine:migrations:diff

I get the error:

The table with name "chkv3.sylius_refund_line_item" already exists.

To bypass this, I manually wrote the migration to add the code field.

  1. However, I still get this error when running:
Call to undefined method Sylius\RefundPlugin\Entity\LineItem::setCode()

This indicates that Sylius is still using the base LineItem instance instead of my custom version.

Investigations:

  1. Am I approaching this the wrong way?
  2. Has anyone successfully overridden LineItem in this plugin?

Thanks for your help!

Upvotes: 0

Views: 21

Answers (0)

Related Questions