I'm working on a Sylius application and want to modify a property of an entity.
To be more concrete: What I want to achieve, is to make the ProductVariant.onHand
(or actually the corresponding column in the database) nullable
.
The documentation of Sylius provides an auspicious article "Customizing Models". But it doesn't describe, how to change the definition of an existing property.
How to modify a property of a Sylius (Core) entity like ProductVariant.onHand
?
What I tried so far: I extended the Sylius\Component\Core\Model\ProductVariant
and added a Doctrine annotation to the onHand
property:
/**
* @ORM\Entity
* @ORM\Table(name="sylius_product_variant")
*/
class ProductVariant extends BaseProductVariant
{
...
/**
* ...
* @ORM\Column(type="integer", nullable=true)
*/
protected $onHand = 0;
...
}
Well, extend
ing the class was definitely a correct step. And it also worked correctly:
$ bin/console debug:container --parameter=sylius.model.product_variant.class
------------------------------------ -----------------------------------
Parameter Value
------------------------------------ -----------------------------------
sylius.model.product_variant.class App\Entity\Product\ProductVariant
------------------------------------ -----------------------------------
But the naïve adding of the property definition led to an error:
$ ./bin/console doctrine:schema:validate
Property "onHand" in "App\Entity\Product\ProductVariant" was already declared, but it must be declared only once
Entity configs can be overridden by adding the AttributeOverrides
annotation:
/**
* @ORM\Entity
* @ORM\Table(name="sylius_product_variant")
* @ORM\AttributeOverrides({
* @ORM\AttributeOverride(
* name="onHand",
* column=@ORM\Column(
* name="on_hand",
* type="integer",
* nullable=true
* )
* )
* })
*/
class ProductVariant extends BaseProductVariant
{
...
/**
* ...
* no changes
*/
protected $onHand;
...
}
Relevant Doctrine docu articles: