Skip to content

Commit

Permalink
Update doctrine configuration (#149)
Browse files Browse the repository at this point in the history
* Use doctrine attributes and remove bundle migratinos

* Update readme and add UPGRADE doc

* Cleanup and fix doctrine mapping

* Rename upgrade file
  • Loading branch information
JoppeDC authored Feb 8, 2024
1 parent 768a83e commit 8983bd2
Show file tree
Hide file tree
Showing 47 changed files with 225 additions and 980 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,9 @@ class OrderItem extends BaseOrderItem implements OrderItemInterface
bin/console sylius:fixtures:load
```

* Finally, update the database and update the translations:
* Finally, generate migrations, update the database and update the translations:
```bash
bin/console doctrine:migrations:diff
bin/console doctrine:migrations:migrate
bin/console translation:update
```
Expand Down
4 changes: 4 additions & 0 deletions UPGRADE-4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Upgrade to 4

- Doctrine entities have been updated to use Attributes instead of XML mappings.
- The bundle no longer includes migrations. These should be generated in your project.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;

final class Brille24SyliusCustomerOptionsExtension extends Extension implements PrependExtensionInterface
final class Brille24SyliusCustomerOptionsExtension extends Extension
{
/**
* @inheritdoc
Expand All @@ -34,25 +34,6 @@ public function load(array $config, ContainerBuilder $container): void
new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config/app'));
}

public function prepend(ContainerBuilder $container): void
{
if (!$container->hasExtension('doctrine_migrations') || !$container->hasExtension('sylius_labs_doctrine_migrations_extra')) {
return;
}

$container->prependExtensionConfig('doctrine_migrations', [
'migrations_paths' => [
'Brille24\SyliusCustomerOptionsPlugin\Migrations' => '@Brille24SyliusCustomerOptionsPlugin/Migrations',
],
]);

$container->prependExtensionConfig('sylius_labs_doctrine_migrations_extra', [
'migrations' => [
'Brille24\SyliusCustomerOptionsPlugin\Migrations' => ['Sylius\Bundle\CoreBundle\Migrations'],
],
]);
}

public function getConfiguration(array $config, ContainerBuilder $container): ConfigurationInterface
{
return new Configuration();
Expand Down
18 changes: 18 additions & 0 deletions src/Entity/CustomerOptions/CustomerOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,52 @@

use Brille24\SyliusCustomerOptionsPlugin\Entity\OrderItemOptionInterface;
use Brille24\SyliusCustomerOptionsPlugin\Enumerations\CustomerOptionTypeEnum;
use Brille24\SyliusCustomerOptionsPlugin\Repository\CustomerOptionRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\JsonType;
use Doctrine\ORM\Mapping as ORM;
use Sylius\Component\Resource\Model\TranslatableTrait;
use Sylius\Component\Resource\Model\TranslationInterface;

#[ORM\Entity(repositoryClass: CustomerOptionRepository::class)]
#[ORM\Table(name: 'brille24_customer_option')]
class CustomerOption implements CustomerOptionInterface
{
use TranslatableTrait {
__construct as protected initializeTranslationsCollection;
getTranslation as private doGetTranslation;
}

#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
#[ORM\Column(type: 'integer')]
protected ?int $id = null;

#[ORM\Column(type: 'string', nullable: false)]
protected string $type = CustomerOptionTypeEnum::SELECT;

#[ORM\Column(type: 'string', unique: true, nullable: false)]
protected ?string $code = '';

#[ORM\Column(type: 'boolean')]
protected bool $required = false;

/** @var Collection|CustomerOptionValueInterface[] */
#[ORM\OneToMany(targetEntity: CustomerOptionValueInterface::class, mappedBy: 'customerOption', orphanRemoval: true, cascade: ['persist', 'remove'])]
#[ORM\OrderBy(['id' => 'ASC'])]
protected Collection $values;

#[ORM\Column(type: 'json')]
protected array $configuration = [];

/** @var Collection|CustomerOptionAssociationInterface[] */
#[ORM\OneToMany(targetEntity: CustomerOptionAssociationInterface::class, mappedBy: 'option', orphanRemoval: true, cascade: ['persist'])]
#[ORM\OrderBy(['position' => 'ASC'])]
protected Collection $groupAssociations;

/** @var Collection|OrderItemOptionInterface[] */
#[ORM\OneToMany(targetEntity: OrderItemOptionInterface::class, mappedBy: 'customerOption')]
protected Collection $orders;

public function __construct()
Expand Down
17 changes: 14 additions & 3 deletions src/Entity/CustomerOptions/CustomerOptionAssociation.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

namespace Brille24\SyliusCustomerOptionsPlugin\Entity\CustomerOptions;

use Doctrine\ORM\Mapping as ORM;

/**
* Class CustomerOptionAssociation
* This class is used as an association between the Customer Option Group and the customer option ordering them by
Expand All @@ -20,17 +22,26 @@
* @see CustomerOption
* @see CustomerOptionGroup
*/
#[ORM\Entity]
#[ORM\Table(name: 'brille24_customer_option_association')]
#[ORM\UniqueConstraint(name: 'option_group_unique', columns: ['option_id', 'group_id'])]
class CustomerOptionAssociation implements CustomerOptionAssociationInterface
{
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
#[ORM\Column(type: 'integer')]
protected ?int $id = null;

#[ORM\ManyToOne(targetEntity: CustomerOptionGroupInterface::class, cascade: ['persist'], inversedBy: 'optionAssociations')]
#[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
protected ?CustomerOptionGroupInterface $group = null;

#[ORM\ManyToOne(targetEntity: CustomerOptionInterface::class, cascade: ['persist'], inversedBy: 'groupAssociations')]
#[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
protected ?CustomerOptionInterface $option = null;

public function __construct(protected int $position = 0)
{
}
#[ORM\Column(type: 'integer')]
protected int $position = 0;

/**
* @inheritdoc
Expand Down
12 changes: 12 additions & 0 deletions src/Entity/CustomerOptions/CustomerOptionGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@

use Brille24\SyliusCustomerOptionsPlugin\Entity\CustomerOptions\Validator\ValidatorInterface;
use Brille24\SyliusCustomerOptionsPlugin\Entity\ProductInterface;
use Brille24\SyliusCustomerOptionsPlugin\Repository\CustomerOptionGroupRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Sylius\Component\Resource\Model\TranslatableTrait;
use Sylius\Component\Resource\Model\TranslationInterface;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: CustomerOptionGroupRepository::class)]
#[ORM\Table(name: 'brille24_customer_option_group')]
class CustomerOptionGroup implements CustomerOptionGroupInterface, \Stringable
{
use TranslatableTrait {
Expand All @@ -27,18 +31,26 @@ class CustomerOptionGroup implements CustomerOptionGroupInterface, \Stringable
}

/** @var int */
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
#[ORM\Column(type: 'integer')]
protected $id;

/** @var string|null */
#[ORM\Column(type: 'string', nullable: true)]
protected $code;

/** @var Collection */
#[ORM\OneToMany(mappedBy: 'group', targetEntity: CustomerOptionAssociationInterface::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
#[ORM\OrderBy(['position' => 'ASC'])]
protected $optionAssociations;

/** @var ArrayCollection */
#[ORM\OneToMany(mappedBy: 'customerOptionGroup', targetEntity: ProductInterface::class)]
protected $products;

/** @var ArrayCollection */
#[ORM\OneToMany(mappedBy: 'customerOptionGroup', targetEntity: ValidatorInterface::class, cascade: ['persist', 'remove'])]
protected $validators;

public function __construct()
Expand Down
12 changes: 12 additions & 0 deletions src/Entity/CustomerOptions/CustomerOptionGroupTranslation.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,26 @@

namespace Brille24\SyliusCustomerOptionsPlugin\Entity\CustomerOptions;

use Doctrine\ORM\Mapping as ORM;
use Sylius\Component\Resource\Model\AbstractTranslation;
use Sylius\Component\Resource\Model\TranslatableInterface;

#[ORM\Entity]
#[ORM\Table(name: 'brille24_customer_option_group_translation')]
class CustomerOptionGroupTranslation extends AbstractTranslation implements CustomerOptionGroupTranslationInterface
{
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
#[ORM\Column(type: 'integer')]
protected ?int $id = null;

#[ORM\Column(type: 'string', nullable: true)]
protected ?string $name = null;

#[ORM\ManyToOne(targetEntity: CustomerOptionGroupInterface::class, inversedBy: "translations")]
#[ORM\JoinColumn(onDelete: "CASCADE")]
protected ?TranslatableInterface $translatable = null;

public function getId(): ?int
{
return $this->id;
Expand Down
12 changes: 12 additions & 0 deletions src/Entity/CustomerOptions/CustomerOptionTranslation.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,26 @@

namespace Brille24\SyliusCustomerOptionsPlugin\Entity\CustomerOptions;

use Doctrine\ORM\Mapping as ORM;
use Sylius\Component\Resource\Model\AbstractTranslation;
use Sylius\Component\Resource\Model\TranslatableInterface;

#[ORM\Entity]
#[ORM\Table(name: 'brille24_customer_option_translation')]
class CustomerOptionTranslation extends AbstractTranslation implements CustomerOptionTranslationInterface
{
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
#[ORM\Column(type: 'integer')]
protected ?int $id = null;

#[ORM\Column(type: 'string', nullable: true)]
protected ?string $name = null;

#[ORM\ManyToOne(targetEntity: CustomerOption::class, inversedBy: 'translations')]
#[ORM\JoinColumn(onDelete: 'CASCADE')]
protected ?TranslatableInterface $translatable = null;

/**
* @inheritdoc
*/
Expand Down
17 changes: 15 additions & 2 deletions src/Entity/CustomerOptions/CustomerOptionValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,48 @@

use Brille24\SyliusCustomerOptionsPlugin\Entity\CustomerOptions\CustomerOptionValuePriceInterface as COValuePriceInterface;
use Brille24\SyliusCustomerOptionsPlugin\Entity\OrderItemOptionInterface;
use Brille24\SyliusCustomerOptionsPlugin\Repository\CustomerOptionValueRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Resource\Model\TranslatableTrait;
use Sylius\Component\Resource\Model\TranslationInterface;

#[ORM\Entity(repositoryClass: CustomerOptionValueRepository::class)]
#[ORM\Table(name: 'brille24_customer_option_value')]
#[ORM\UniqueConstraint(name: 'unique_customer_option_code', columns: ['customerOption_id', 'code'])]
class CustomerOptionValue implements CustomerOptionValueInterface, \Stringable
{
use TranslatableTrait {
__construct as protected initializeTranslationsCollection;
getTranslation as private doGetTranslation;
}

#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
#[ORM\Column(type: 'integer')]
protected ?int $id = null;

#[ORM\Column(type: 'string')]
protected string $code;

#[ORM\OneToMany(mappedBy: 'customerOptionValue', targetEntity: COValuePriceInterface::class, cascade: ['persist', 'remove'])]
protected Collection $prices;

#[ORM\ManyToOne(targetEntity: CustomerOptionInterface::class, inversedBy: 'values')]
#[ORM\JoinColumn(onDelete: 'CASCADE')]
protected ?CustomerOptionInterface $customerOption = null;

/** @var OrderItemOptionInterface[] */
protected $orders;
#[ORM\OneToMany(mappedBy: 'customerOptionValue', targetEntity: OrderItemOptionInterface::class)]
protected Collection $orders;

public function __construct()
{
$this->initializeTranslationsCollection();
$this->prices = new ArrayCollection();
$this->orders = new ArrayCollection();
}

/**
Expand Down
18 changes: 18 additions & 0 deletions src/Entity/CustomerOptions/CustomerOptionValuePrice.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,46 @@
namespace Brille24\SyliusCustomerOptionsPlugin\Entity\CustomerOptions;

use Brille24\SyliusCustomerOptionsPlugin\Entity\ProductInterface;
use Brille24\SyliusCustomerOptionsPlugin\Entity\Tools\DateRange;
use Brille24\SyliusCustomerOptionsPlugin\Entity\Tools\DateRangeInterface;
use Doctrine\ORM\Mapping as ORM;
use Sylius\Bundle\MoneyBundle\Formatter\MoneyFormatter;
use Sylius\Bundle\MoneyBundle\Formatter\MoneyFormatterInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Symfony\Component\Config\Definition\Exception\InvalidTypeException;

#[ORM\Entity]
#[ORM\Table(name: 'brille24_customer_option_value_price')]
class CustomerOptionValuePrice implements CustomerOptionValuePriceInterface, \Stringable
{
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
#[ORM\Column(type: 'integer')]
protected ?int $id = null;

#[ORM\Column(type: 'float')]
protected float $percent = 0;

#[ORM\Column(type: 'integer')]
protected int $amount = 0;

#[ORM\Column(type: 'string', length: 12)]
protected string $type = CustomerOptionValuePriceInterface::TYPE_FIXED_AMOUNT;

#[ORM\ManyToOne(targetEntity: CustomerOptionValueInterface::class, cascade: ['persist'], inversedBy: 'prices')]
#[ORM\JoinColumn(onDelete: 'CASCADE')]
protected ?CustomerOptionValueInterface $customerOptionValue = null;

#[ORM\ManyToOne(targetEntity: ChannelInterface::class)]
#[ORM\JoinColumn(onDelete: 'CASCADE')]
protected ?ProductInterface $product = null;

#[ORM\ManyToOne(targetEntity: ChannelInterface::class)]
#[ORM\JoinColumn(onDelete: 'CASCADE')]
protected ?ChannelInterface $channel = null;

#[ORM\OneToOne(targetEntity: DateRange::class, cascade: ['all'])]
#[ORM\JoinColumn(nullable: true)]
protected ?DateRangeInterface $dateValid = null;

public function getId(): ?int
Expand Down
12 changes: 12 additions & 0 deletions src/Entity/CustomerOptions/CustomerOptionValueTranslation.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,26 @@

namespace Brille24\SyliusCustomerOptionsPlugin\Entity\CustomerOptions;

use Doctrine\ORM\Mapping as ORM;
use Sylius\Component\Resource\Model\AbstractTranslation;
use Sylius\Component\Resource\Model\TranslatableInterface;

#[ORM\Entity]
#[ORM\Table(name: 'brille24_customer_option_value_translation')]
class CustomerOptionValueTranslation extends AbstractTranslation implements CustomerOptionValueTranslationInterface
{
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
#[ORM\Column(type: 'integer')]
protected ?int $id = null;

#[ORM\Column(type: 'string', nullable: true)]
protected ?string $name = null;

#[ORM\ManyToOne(targetEntity: CustomerOptionValueInterface::class, inversedBy: "translations")]
#[ORM\JoinColumn(onDelete: 'CASCADE')]
protected ?TranslatableInterface $translatable = null;

/**
* @inheritdoc
*/
Expand Down
6 changes: 6 additions & 0 deletions src/Entity/CustomerOptions/Validator/Condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@
namespace Brille24\SyliusCustomerOptionsPlugin\Entity\CustomerOptions\Validator;

use Brille24\SyliusCustomerOptionsPlugin\Traits\ConditionTrait;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[ORM\Table(name: 'brille24_customer_option_group_validator_condition')]
class Condition implements ConditionInterface
{
use ConditionTrait;

#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
protected ?int $id = null;

public function getId(): ?int
Expand Down
Loading

0 comments on commit 8983bd2

Please sign in to comment.