Annotation @Enum was never imported in Sylius product association ajax call

I’ve recently been building an ecommerce site for the family home textile business, as you do. When choosing the platform, my main criteria were that it should be based on Symfony or Laravel, and that it definitely shouldn’t be Magento. I think I’ve already alluded to my thoughts on Magento 2 being a hulking, bloated and buggy mess in a previous post, and might well expand on that in future. It’s certainly overkill for most ecommerce builds.

I eventually settled on Sylius - the project is mature and active, has decent traction, and the code quality is reassuringly high.

Sylius

And so far, so good. It has a small but decent plugin ecosystem covering many common use cases, and it’s relatively straight forward to customise. For example, I had to implement pricing and shipping calculators for made to measure curtains with varying fabrics, and it didn’t take too long to get my head round a suitable architecture for this.

The Sylius documentation is rather out of date in places, but the code is very well organised and easy to understand - it’s all just Symfony under the hood - plus there are active Slack and forum channels with a very helpful community if you run into problems.

Sylius on Slack

I did come across one tricky to debug issue when trying to fetch product associations on the staging instance, while we were in the process of inputting the initial product content. The product association search in the admin panel suddenly stopped working completely, spinning circle of doom time.

Inspecting the ajax response threw up the following error:

[Semantical Error] The annotation "@enum" in property Doctrine\ORM\Mapping\ManyToOne::$fetch was never imported. Did you maybe forget to add a "use" statement for this annotation?

So far so unhelpful, this error message didn’t really shed much light on anything. Apart from Doctrine, the stack trace was also full of references to JMS Serializer. After some fruitless var dumping in the referenced classes, and a few rudderless Stack Overflow searches, I started to wonder whether this was more of a side effect than the main issue.

I’m using a customised and extended version of the excellent Brille24 Custom Options plugin, which further investigation led me to believe might have something to do with it - there were some test orders in the system, and when I deleted the custom order item options from the database the problem went away.

Possibly one of my somewhat hacky customisations might have had something to do with surfacing this issue? To be honest, I’m still not entirely sure! But I did start to question why Product search via associations would actually need to load anything to do with orders, so I suspected I might have to try to restrict which relations are loaded during the ajax call somehow.

The solution turned out to be quite simple. Sylius uses JMS Serializer to render entities as json in ajax calls. After scouring the documentation for the bundle, I discovered the annotations syntax for exclusion strategies.

I added the following annotation to my Product entity class, which basically instructs the serializer not to serialise every single object property:

use JMS\Serializer\Annotation\ExclusionPolicy;
...

/**
...
 * @ExclusionPolicy("all")
 */

 class Product extends BaseProduct implements ProductInterface
{
...

And… problem solved! The product association search also started running a little faster than it did previously, which tended to confirm my suspicions about data being loaded too eagerly somewhere along the chain.

Further investigation regarding the speed bottleneck eventually also brought me to the following unrelated (afaik) github issue/fix: https://github.com/Sylius/Sylius/issues/9364#issuecomment-623447669

With both of these in place, the search now runs smooth as butter.

I’m also happy to say that the store has since launched and is successfully taking orders. Check it out at www.graceandgoldney.co.uk

Grace & Goldney Product Page