Shopware 6: Entity Extension without properties?!

Shopware Version: 6.5.3.3

Today morning we sat with three of our developers together to debug a weird behaviour of one of our plugins.

Let me tell you: If you need three people to debug weird behaviour, which normally is no problem and you just write it down, it is always the small things.

The problem

We extended the customer and wanted to log certain actions, something like this:

<?php

namespace Winkelwagen\CustomerLogging\EntityExtension;

use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\Framework\DataAbstractionLayer\Entity;
use Shopware\Core\Framework\DataAbstractionLayer\EntityIdTrait;

class CustomerLoggingExtensionEntity extends Entity
{
    use EntityIdTrait;
    private string $type;
    protected string $customerId;
    private array $payload;
    private CustomerEntity $customer;

    // getter and setter
}

Nothing interesting.

When we load a customer and add the association customerLogs, we got an extension on the customer, containing a collection, containing lots of CustomerLoggingExtensionEntitys, but none of them contained a payload or type. The colleague already tried to find the cause for a while, but nothing.

So we added two other pairs of eyes to spot the problem. What was not the problem:

  • The entity extension was defined correctly
  • the definition was okay
  • the database contains data
  • the relationshops in the definition are okay
  • no typos, wrong classes, etc.

We were kind of stuck ?

The solution

Then one of us fall the scales from their eyes! Why did the extension contains the customerId and the id it self?

Have you spotted the problem in the code above? Entities have protected properties. I don’t know what happens under the hood in detail, but it seems because the \Shopware\Core\Framework\DataAbstractionLayer\Entity is doing the database work, it must have access to the properties to write them.

<?php

namespace Winkelwagen\CustomerLogging\EntityExtension;

use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\Framework\DataAbstractionLayer\Entity;
use Shopware\Core\Framework\DataAbstractionLayer\EntityIdTrait;

class CustomerLoggingExtensionEntity extends Entity
{
    use EntityIdTrait;
    protected string $type;
    protected string $customerId;
    protected array $payload;
    protected CustomerEntity $customer;

    // getter and setter
}

A team makes a developer a 10x developer

Maybe the colleague have found the problem in 5min as well, most likely not. A team helps you in situation if you are already blind for the code in front of you.

And another tipp: If you help people debug, let them explain what they do, listen and make sure this happens. AND: Don’t assume! Always check. Typos, database content, etc.

One thought on “Shopware 6: Entity Extension without properties?!

Leave a Reply