| .. | ||
| how-to-add-a-custom-model-accessible-for-respective-channel-administrators.md | ||
| how-to-add-a-custom-model.md | ||
| how-to-add-a-custom-translatable-model.md | ||
| README.md | ||
| layout | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Customizing Models
In Sylius, all models are located in the Sylius\Component\*ComponentName*\Model namespaces with corresponding interfaces. Many models are also extended within the Core component. If you want to customize a model that exists in Core, make sure to extend the Core version rather than the base model from the component itself.
{% hint style="info" %} Some models in Sylius are translatable, which means they support multiple languages. This guide covers customization for both regular and translatable models. {% endhint %}
Why customize a model?
You might want to customize models to meet specific business needs. Here are a few examples:
- Add a
flagfield to theCountrymodel. - Add
secondNumberto theCustomermodel. - Modify
reviewSubjectin theReviewmodel. - Add an
iconto thePaymentMethodmodel.
How to customize a model?
Example: Adding a flag Field to the Country Model
In this example, we will customize the Country model by adding a flag field to store an image URL.
-
Extend the Model
Insrc/Entity/Addressing/Country.php, extend theSylius\Component\Addressing\Model\Countryclass.<?php declare(strict_types=1); namespace App\Entity\Addressing; use Doctrine\ORM\Mapping as ORM; use Sylius\Component\Addressing\Model\Country as BaseCountry; use Sylius\Component\Addressing\Model\CountryInterface; /** * @ORM\Entity() * @ORM\Table(name="sylius_country") */ class Country extends BaseCountry implements CountryInterface { /** @ORM\Column(type="string", nullable=true) */ private $flag; public function getFlag(): ?string { return $this->flag; } public function setFlag(string $flag): void { $this->flag = $flag; } } -
Configure the Model in
config/packages/_sylius.yaml
Under thesylius_addressingsection, ensure the custom model is referenced correctly.sylius_addressing: resources: country: classes: model: App\Entity\Addressing\Country -
Update the Database
Choose one of these two methods:-
Direct Schema Update (not recommended for production):
php bin/console doctrine:schema:update --force -
Migrations (recommended):
php bin/console doctrine:migrations:diff php bin/console doctrine:migrations:migrate
-
-
Add the Field in the Admin Panel (Optional)
To make the newflagfield editable in the admin panel, you’ll need to update its form type. Check here.
How to customize a translatable model?
Translatable models, like ShippingMethod, support multilingual content. In this example, we’ll add an estimatedDeliveryTime field to ShippingMethod.
Example: Adding an estimatedDeliveryTime Field to the ShippingMethod Model
-
Extend the Model
Insrc/Entity/Shipping/ShippingMethod.php, extend theSylius\Component\Core\Model\ShippingMethodclass.<?php declare(strict_types=1); namespace App\Entity\Shipping; use Doctrine\ORM\Mapping as ORM; use Sylius\Component\Core\Model\ShippingMethod as BaseShippingMethod; use Sylius\Component\Core\Model\ShippingMethodInterface; use Sylius\Component\Shipping\Model\ShippingMethodTranslationInterface; /** * @ORM\Entity() * @ORM\Table(name="sylius_shipping_method") */ class ShippingMethod extends BaseShippingMethod implements ShippingMethodInterface { /** @ORM\Column(type="string", nullable=true) */ private $estimatedDeliveryTime; public function getEstimatedDeliveryTime(): ?string { return $this->estimatedDeliveryTime; } public function setEstimatedDeliveryTime(?string $estimatedDeliveryTime): void { $this->estimatedDeliveryTime = $estimatedDeliveryTime; } protected function createTranslation(): ShippingMethodTranslationInterface { return new ShippingMethodTranslation(); } } -
Configure the Model in
_sylius.yaml
Updateconfig/packages/_sylius.yamlwith the following configuration:sylius_shipping: resources: shipping_method: classes: model: App\Entity\Shipping\ShippingMethod -
Update the Database
Run the migrations:php bin/console doctrine:migrations:diff php bin/console doctrine:migrations:migrate -
Add the Field in the Admin Panel (Optional)
If you want administrators to editestimatedDeliveryTime, update the form type accordingly. See form type customization here.
Example: Adding a translatable deliveryConditions Field to the ShippingMethod Model
Let’s assume you want to add a multilingual deliveryConditions field to ShippingMethod.
-
Extend the Translation Class
Insrc/Entity/Shipping/ShippingMethodTranslation.php, extendShippingMethodTranslationand add thedeliveryConditionsfield.<?php declare(strict_types=1); namespace App\Entity\Shipping; use Doctrine\ORM\Mapping as ORM; use Sylius\Component\Shipping\Model\ShippingMethodTranslation as BaseShippingMethodTranslation; use Sylius\Component\Shipping\Model\ShippingMethodTranslationInterface; /** * @ORM\Entity() * @ORM\Table(name="sylius_shipping_method_translation") */ class ShippingMethodTranslation extends BaseShippingMethodTranslation implements ShippingMethodTranslationInterface { /** @ORM\Column(type="string", nullable=true) */ private $deliveryConditions; public function getDeliveryConditions(): ?string { return $this->deliveryConditions; } public function setDeliveryConditions(?string $deliveryConditions): void { $this->deliveryConditions = $deliveryConditions; } } -
Add Access Methods in
ShippingMethod
Modifysrc/Entity/Shipping/ShippingMethod.phpto get and setdeliveryConditions:public function getDeliveryConditions(): ?string { return $this->getTranslation()->getDeliveryConditions(); } public function setDeliveryConditions(?string $deliveryConditions): void { $this->getTranslation()->setDeliveryConditions($deliveryConditions); } -
Configure in
_sylius.yaml
Updateconfig/packages/_sylius.yamlto include both the main model and its translation.sylius_shipping: resources: shipping_method: classes: model: App\Entity\Shipping\ShippingMethod translation: classes: model: App\Entity\Shipping\ShippingMethodTranslation -
Database Update
Run migrations as before:php bin/console doctrine:migrations:diff php bin/console doctrine:migrations:migrate
Key Takeaways
- Parameter Configuration: Sylius automatically uses the customized model in parameters, repositories, and controllers.
- Translatable Models: Add properties to both the main and translation entities, and remember to update configuration and migrations.
- Form Types: Update forms if you want your new fields editable in the admin.
- Customizations can be done in your application or within Sylius Plugins.