diff --git a/src/Entity/TierPrice.php b/src/Entity/TierPrice.php index 9b3cb4e..5a2a090 100644 --- a/src/Entity/TierPrice.php +++ b/src/Entity/TierPrice.php @@ -14,22 +14,40 @@ namespace Brille24\SyliusTierPricePlugin\Entity; +use Brille24\SyliusTierPricePlugin\Repository\TierPriceRepository; +use Doctrine\ORM\Mapping\Column; +use Doctrine\ORM\Mapping\Entity; +use Doctrine\ORM\Mapping\Id; +use Doctrine\ORM\Mapping\ManyToOne; +use Doctrine\ORM\Mapping\Table; +use Doctrine\ORM\Mapping\UniqueConstraint; use Sylius\Component\Core\Model\ChannelInterface; use Sylius\Component\Customer\Model\CustomerGroupInterface; +#[Entity(repositoryClass:TierPriceRepository::class)] +#[Table(name: 'brille24_tierprice')] +#[UniqueConstraint(name: 'no_duplicate_prices', columns: ['qty','channel_id','product_variant_id','customer_group_id'])] class TierPrice implements TierPriceInterface { /** @var int */ + #[Id, Column(type: 'integer')] private $id; + #[ManyToOne(targetEntity: ChannelInterface::class)] private ?ChannelInterface $channel = null; + #[ManyToOne(targetEntity: ProductVariantInterface::class, inversedBy: 'tierPrices')] private ProductVariantInterface $productVariant; + #[ManyToOne(targetEntity: CustomerGroupInterface::class, inversedBy: 'customerGroup')] private ?CustomerGroupInterface $customerGroup = null; - public function __construct(private int $qty = 0, private int $price = 0) - { + public function __construct( + #[Column(type: 'integer')] + private int $qty = 0, + #[Column(type: 'integer')] + private int $price = 0 + ) { } public function getId(): ?int diff --git a/src/Traits/TierPriceableTrait.php b/src/Traits/TierPriceableTrait.php index 599662d..46af77d 100644 --- a/src/Traits/TierPriceableTrait.php +++ b/src/Traits/TierPriceableTrait.php @@ -15,8 +15,11 @@ namespace Brille24\SyliusTierPricePlugin\Traits; use Brille24\SyliusTierPricePlugin\Entity\ProductVariant; +use Brille24\SyliusTierPricePlugin\Entity\TierPrice; use Brille24\SyliusTierPricePlugin\Entity\TierPriceInterface; use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\ORM\Mapping\OneToMany; +use Doctrine\ORM\Mapping\OrderBy; use Sylius\Component\Core\Model\ChannelInterface; use Sylius\Component\Core\Model\CustomerInterface; use Sylius\Component\Core\Model\ProductVariantInterface; @@ -37,6 +40,8 @@ public function initTierPriceableTrait(): void } /** @var ArrayCollection */ + #[OneToMany(TierPrice::class, mappedBy: "productVariant", orphanRemoval: true, cascade: ['all'])] + #[OrderBy(['customerGroup' => 'ASC', 'qty' => 'ASC'])] protected $tierPrices; /**