Skip to content

Commit 8983bd2

Browse files
authored
Update doctrine configuration (#149)
* Use doctrine attributes and remove bundle migratinos * Update readme and add UPGRADE doc * Cleanup and fix doctrine mapping * Rename upgrade file
1 parent 768a83e commit 8983bd2

File tree

47 files changed

+225
-980
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+225
-980
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,9 @@ class OrderItem extends BaseOrderItem implements OrderItemInterface
120120
bin/console sylius:fixtures:load
121121
```
122122

123-
* Finally, update the database and update the translations:
123+
* Finally, generate migrations, update the database and update the translations:
124124
```bash
125+
bin/console doctrine:migrations:diff
125126
bin/console doctrine:migrations:migrate
126127
bin/console translation:update
127128
```

UPGRADE-4.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Upgrade to 4
2+
3+
- Doctrine entities have been updated to use Attributes instead of XML mappings.
4+
- The bundle no longer includes migrations. These should be generated in your project.

src/DependencyInjection/Brille24SyliusCustomerOptionsExtension.php

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
2020
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
2121

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

37-
public function prepend(ContainerBuilder $container): void
38-
{
39-
if (!$container->hasExtension('doctrine_migrations') || !$container->hasExtension('sylius_labs_doctrine_migrations_extra')) {
40-
return;
41-
}
42-
43-
$container->prependExtensionConfig('doctrine_migrations', [
44-
'migrations_paths' => [
45-
'Brille24\SyliusCustomerOptionsPlugin\Migrations' => '@Brille24SyliusCustomerOptionsPlugin/Migrations',
46-
],
47-
]);
48-
49-
$container->prependExtensionConfig('sylius_labs_doctrine_migrations_extra', [
50-
'migrations' => [
51-
'Brille24\SyliusCustomerOptionsPlugin\Migrations' => ['Sylius\Bundle\CoreBundle\Migrations'],
52-
],
53-
]);
54-
}
55-
5637
public function getConfiguration(array $config, ContainerBuilder $container): ConfigurationInterface
5738
{
5839
return new Configuration();

src/Entity/CustomerOptions/CustomerOption.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,52 @@
1414

1515
use Brille24\SyliusCustomerOptionsPlugin\Entity\OrderItemOptionInterface;
1616
use Brille24\SyliusCustomerOptionsPlugin\Enumerations\CustomerOptionTypeEnum;
17+
use Brille24\SyliusCustomerOptionsPlugin\Repository\CustomerOptionRepository;
1718
use Doctrine\Common\Collections\ArrayCollection;
1819
use Doctrine\Common\Collections\Collection;
20+
use Doctrine\DBAL\Types\JsonType;
21+
use Doctrine\ORM\Mapping as ORM;
1922
use Sylius\Component\Resource\Model\TranslatableTrait;
2023
use Sylius\Component\Resource\Model\TranslationInterface;
2124

25+
#[ORM\Entity(repositoryClass: CustomerOptionRepository::class)]
26+
#[ORM\Table(name: 'brille24_customer_option')]
2227
class CustomerOption implements CustomerOptionInterface
2328
{
2429
use TranslatableTrait {
2530
__construct as protected initializeTranslationsCollection;
2631
getTranslation as private doGetTranslation;
2732
}
2833

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

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

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

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

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

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

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

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

4765
public function __construct()

src/Entity/CustomerOptions/CustomerOptionAssociation.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
namespace Brille24\SyliusCustomerOptionsPlugin\Entity\CustomerOptions;
1414

15+
use Doctrine\ORM\Mapping as ORM;
16+
1517
/**
1618
* Class CustomerOptionAssociation
1719
* This class is used as an association between the Customer Option Group and the customer option ordering them by
@@ -20,17 +22,26 @@
2022
* @see CustomerOption
2123
* @see CustomerOptionGroup
2224
*/
25+
#[ORM\Entity]
26+
#[ORM\Table(name: 'brille24_customer_option_association')]
27+
#[ORM\UniqueConstraint(name: 'option_group_unique', columns: ['option_id', 'group_id'])]
2328
class CustomerOptionAssociation implements CustomerOptionAssociationInterface
2429
{
30+
#[ORM\Id]
31+
#[ORM\GeneratedValue(strategy: 'AUTO')]
32+
#[ORM\Column(type: 'integer')]
2533
protected ?int $id = null;
2634

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

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

31-
public function __construct(protected int $position = 0)
32-
{
33-
}
43+
#[ORM\Column(type: 'integer')]
44+
protected int $position = 0;
3445

3546
/**
3647
* @inheritdoc

src/Entity/CustomerOptions/CustomerOptionGroup.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,15 @@
1414

1515
use Brille24\SyliusCustomerOptionsPlugin\Entity\CustomerOptions\Validator\ValidatorInterface;
1616
use Brille24\SyliusCustomerOptionsPlugin\Entity\ProductInterface;
17+
use Brille24\SyliusCustomerOptionsPlugin\Repository\CustomerOptionGroupRepository;
1718
use Doctrine\Common\Collections\ArrayCollection;
1819
use Doctrine\Common\Collections\Collection;
1920
use Sylius\Component\Resource\Model\TranslatableTrait;
2021
use Sylius\Component\Resource\Model\TranslationInterface;
22+
use Doctrine\ORM\Mapping as ORM;
2123

24+
#[ORM\Entity(repositoryClass: CustomerOptionGroupRepository::class)]
25+
#[ORM\Table(name: 'brille24_customer_option_group')]
2226
class CustomerOptionGroup implements CustomerOptionGroupInterface, \Stringable
2327
{
2428
use TranslatableTrait {
@@ -27,18 +31,26 @@ class CustomerOptionGroup implements CustomerOptionGroupInterface, \Stringable
2731
}
2832

2933
/** @var int */
34+
#[ORM\Id]
35+
#[ORM\GeneratedValue(strategy: 'AUTO')]
36+
#[ORM\Column(type: 'integer')]
3037
protected $id;
3138

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

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

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

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

4456
public function __construct()

src/Entity/CustomerOptions/CustomerOptionGroupTranslation.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,26 @@
1212

1313
namespace Brille24\SyliusCustomerOptionsPlugin\Entity\CustomerOptions;
1414

15+
use Doctrine\ORM\Mapping as ORM;
1516
use Sylius\Component\Resource\Model\AbstractTranslation;
17+
use Sylius\Component\Resource\Model\TranslatableInterface;
1618

19+
#[ORM\Entity]
20+
#[ORM\Table(name: 'brille24_customer_option_group_translation')]
1721
class CustomerOptionGroupTranslation extends AbstractTranslation implements CustomerOptionGroupTranslationInterface
1822
{
23+
#[ORM\Id]
24+
#[ORM\GeneratedValue(strategy: 'AUTO')]
25+
#[ORM\Column(type: 'integer')]
1926
protected ?int $id = null;
2027

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

31+
#[ORM\ManyToOne(targetEntity: CustomerOptionGroupInterface::class, inversedBy: "translations")]
32+
#[ORM\JoinColumn(onDelete: "CASCADE")]
33+
protected ?TranslatableInterface $translatable = null;
34+
2335
public function getId(): ?int
2436
{
2537
return $this->id;

src/Entity/CustomerOptions/CustomerOptionTranslation.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,26 @@
1212

1313
namespace Brille24\SyliusCustomerOptionsPlugin\Entity\CustomerOptions;
1414

15+
use Doctrine\ORM\Mapping as ORM;
1516
use Sylius\Component\Resource\Model\AbstractTranslation;
17+
use Sylius\Component\Resource\Model\TranslatableInterface;
1618

19+
#[ORM\Entity]
20+
#[ORM\Table(name: 'brille24_customer_option_translation')]
1721
class CustomerOptionTranslation extends AbstractTranslation implements CustomerOptionTranslationInterface
1822
{
23+
#[ORM\Id]
24+
#[ORM\GeneratedValue(strategy: 'AUTO')]
25+
#[ORM\Column(type: 'integer')]
1926
protected ?int $id = null;
2027

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

31+
#[ORM\ManyToOne(targetEntity: CustomerOption::class, inversedBy: 'translations')]
32+
#[ORM\JoinColumn(onDelete: 'CASCADE')]
33+
protected ?TranslatableInterface $translatable = null;
34+
2335
/**
2436
* @inheritdoc
2537
*/

src/Entity/CustomerOptions/CustomerOptionValue.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,48 @@
1414

1515
use Brille24\SyliusCustomerOptionsPlugin\Entity\CustomerOptions\CustomerOptionValuePriceInterface as COValuePriceInterface;
1616
use Brille24\SyliusCustomerOptionsPlugin\Entity\OrderItemOptionInterface;
17+
use Brille24\SyliusCustomerOptionsPlugin\Repository\CustomerOptionValueRepository;
1718
use Doctrine\Common\Collections\ArrayCollection;
1819
use Doctrine\Common\Collections\Collection;
20+
use Doctrine\ORM\Mapping as ORM;
1921
use Sylius\Component\Core\Model\ChannelInterface;
2022
use Sylius\Component\Core\Model\ProductInterface;
2123
use Sylius\Component\Resource\Model\TranslatableTrait;
2224
use Sylius\Component\Resource\Model\TranslationInterface;
2325

26+
#[ORM\Entity(repositoryClass: CustomerOptionValueRepository::class)]
27+
#[ORM\Table(name: 'brille24_customer_option_value')]
28+
#[ORM\UniqueConstraint(name: 'unique_customer_option_code', columns: ['customerOption_id', 'code'])]
2429
class CustomerOptionValue implements CustomerOptionValueInterface, \Stringable
2530
{
2631
use TranslatableTrait {
2732
__construct as protected initializeTranslationsCollection;
2833
getTranslation as private doGetTranslation;
2934
}
3035

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

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

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

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

39-
/** @var OrderItemOptionInterface[] */
40-
protected $orders;
51+
#[ORM\OneToMany(mappedBy: 'customerOptionValue', targetEntity: OrderItemOptionInterface::class)]
52+
protected Collection $orders;
4153

4254
public function __construct()
4355
{
4456
$this->initializeTranslationsCollection();
4557
$this->prices = new ArrayCollection();
58+
$this->orders = new ArrayCollection();
4659
}
4760

4861
/**

src/Entity/CustomerOptions/CustomerOptionValuePrice.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,46 @@
1313
namespace Brille24\SyliusCustomerOptionsPlugin\Entity\CustomerOptions;
1414

1515
use Brille24\SyliusCustomerOptionsPlugin\Entity\ProductInterface;
16+
use Brille24\SyliusCustomerOptionsPlugin\Entity\Tools\DateRange;
1617
use Brille24\SyliusCustomerOptionsPlugin\Entity\Tools\DateRangeInterface;
18+
use Doctrine\ORM\Mapping as ORM;
1719
use Sylius\Bundle\MoneyBundle\Formatter\MoneyFormatter;
1820
use Sylius\Bundle\MoneyBundle\Formatter\MoneyFormatterInterface;
1921
use Sylius\Component\Core\Model\ChannelInterface;
2022
use Symfony\Component\Config\Definition\Exception\InvalidTypeException;
2123

24+
#[ORM\Entity]
25+
#[ORM\Table(name: 'brille24_customer_option_value_price')]
2226
class CustomerOptionValuePrice implements CustomerOptionValuePriceInterface, \Stringable
2327
{
28+
#[ORM\Id]
29+
#[ORM\GeneratedValue(strategy: 'AUTO')]
30+
#[ORM\Column(type: 'integer')]
2431
protected ?int $id = null;
2532

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

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

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

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

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

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

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

4058
public function getId(): ?int

0 commit comments

Comments
 (0)