添加新实体Sylius

时间:2019-12-27 12:01:22

标签: sylius

我在App \ Entity \中创建了新实体,并出现以下错误:

  

编译错误:App \ Entity \ OrderItem :: getVariant()的声明:?App \ Entity \ ProductVariantInterface必须与Sylius \ Component \ Core \ Model \ OrderItem :: getVariant():?Sylius \ Component \ Core兼容\ Model \ ProductVariantInterface

这是我的OrderItem.php:

<?php

namespace App\Entity;

use App\Entity\Constants\DeliveryLocationConstants;
use Sylius\Component\Core\Model\OrderItem as BaseOrderItem;
use Doctrine\ORM\Mapping as ORM;
use App\Entity\OrderItemInterface as BaseOrderItemInterface;

/**
 * @ORM\Entity()
 * @ORM\Table(name="sylius_order_item")
 *
 * Class OrderItem
 * @package App\Entity
 */
class OrderItem extends BaseOrderItem implements BaseOrderItemInterface
{
    /** @var ProductVariantInterface */
    protected $variant;

    /** @var string */
    protected $productName;

    /** @var string */
    protected $variantName;

    /** @var string */
    protected $optiune;

    /**
     * {@inheritdoc}
     */
    public function getVariant(): ?ProductVariantInterface
    {
        return $this->variant;
    }

    /**
     * {@inheritdoc}
     */
    public function setVariant(?ProductVariantInterface $variant): void
    {
        $this->variant = $variant;
    }

    public function getProduct(): ?ProductInterface
    {
        return $this->variant->getProduct();
    }

    /**
     * {@inheritdoc}
     */
    public function getProductName(): ?string
    {
        return $this->productName ?: $this->variant->getProduct()->getName();
    }

    /**
     * {@inheritdoc}
     */
    public function getOptiune(): ?string
    {
        return $this->optiune;
    }

    /**
     * {@inheritdoc}
     */
    public function setOptiune(?string $optiune): void
    {
        $this->optiune = $optiune;
    }

    /**
     * {@inheritdoc}
     */
    public function setProductName(?string $productName): void
    {
        $this->productName = $productName;
    }

    /**
     * {@inheritdoc}
     */
    public function getVariantName(): ?string
    {
        return $this->variantName ?: $this->variant->getName();
    }

    /**
     * {@inheritdoc}
     */
    public function setVariantName(?string $variantName): void
    {
        $this->variantName = $variantName;
    }

    /**
     * {@inheritdoc}
     */
    public function equals(BaseOrderItemInterface $item): bool
    {
        return parent::equals($item) || ($item instanceof static && $item->getVariant() === $this->variant);
    }

    /**
     * Returns sum of neutral and non neutral tax adjustments on order item and total tax of units.
     *
     * {@inheritdoc}
     */
    public function getTaxTotal(): int
    {
        $taxTotal = 0;

        foreach ($this->getAdjustments(AdjustmentInterface::TAX_ADJUSTMENT) as $taxAdjustment) {
            $taxTotal += $taxAdjustment->getAmount();
        }

        foreach ($this->units as $unit) {
            $taxTotal += $unit->getTaxTotal();
        }

        return $taxTotal;
    }

    /**
     * Returns single unit price lowered by order unit promotions (each unit must have the same unit promotion discount)
     *
     * {@inheritdoc}
     */
    public function getDiscountedUnitPrice(): int
    {
        if ($this->units->isEmpty()) {
            return $this->unitPrice;
        }

        return
            $this->unitPrice +
            $this->units->first()->getAdjustmentsTotal(AdjustmentInterface::ORDER_UNIT_PROMOTION_ADJUSTMENT)
        ;
    }

    /**
     * {@inheritdoc}
     */
    public function getSubtotal(): int
    {
        return $this->getDiscountedUnitPrice() * $this->quantity;
    }
}

1 个答案:

答案 0 :(得分:0)

错误消息很清楚。您必须像这样重新定义getVariant()方法:

    public function getVariant(): ?\Sylius\Component\Core\Model\ProductVariantInterface
    {
        return $this->variant;
    }

因为在PHP父类BaseOrderItem中,您坚持使用此方法返回Core \ Model \ ProductVariantInterface