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.
Some models in Sylius are translatable, which means they support multiple languages. This guide covers customization for both regular and translatable models.
Why customize a model?
You might want to customize models to meet specific business needs. Here are a few examples:
Add a
flag
field to theCountry
model.Add
secondNumber
to theCustomer
model.Modify
reviewSubject
in theReview
model.Add an
icon
to thePaymentMethod
model.
How to customize a model?
Example: Adding a flag
Field to the Country
Model
flag
Field to the Country
ModelIn this example, we will customize the Country
model by adding a flag
field to store an image URL.
Extend the Model In
src/Entity/Addressing/Country.php
, extend theSylius\Component\Addressing\Model\Country
class.Configure the Model in
config/packages/_sylius.yaml
Under thesylius_addressing
section, ensure the custom model is referenced correctly.Update the Database Choose one of these two methods:
Direct Schema Update (not recommended for production):
Migrations (recommended):
Add the Field in the Admin Panel (Optional) To make the new
flag
field 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
estimatedDeliveryTime
Field to the ShippingMethod
ModelExtend the Model In
src/Entity/Shipping/ShippingMethod.php
, extend theSylius\Component\Core\Model\ShippingMethod
class.Configure the Model in
_sylius.yaml
Updateconfig/packages/_sylius.yaml
with the following configuration:Update the Database Run the migrations:
Add the Field in the Admin Panel (Optional) If you want administrators to edit
estimatedDeliveryTime
, update the form type accordingly. See form type customization here.
Example: Adding a translatable deliveryConditions
Field to the ShippingMethod
Model
deliveryConditions
Field to the ShippingMethod
ModelLet’s assume you want to add a multilingual deliveryConditions
field to ShippingMethod
.
Extend the Translation Class In
src/Entity/Shipping/ShippingMethodTranslation.php
, extendShippingMethodTranslation
and add thedeliveryConditions
field.Add Access Methods in
ShippingMethod
Modifysrc/Entity/Shipping/ShippingMethod.php
to get and setdeliveryConditions
:Configure in
_sylius.yaml
Updateconfig/packages/_sylius.yaml
to include both the main model and its translation.Database Update Run migrations as before:
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.