Search code examples
shopwareshopware6

Shopware 6: Extend existing core DAL entities with a reference


I'm trying to create a connection between 2 existing entities PropertyGroup and CustomFieldSet. Use-case is irrelevant.

So I created an EntityExtension:

    public function extendFields(FieldCollection $collection): void
    {
        $collection->add(
            (new ManyToOneAssociationField('customFieldSet', 'custom_field_set', CustomFieldSetDefinition::class))
        );
    }

    public function getDefinitionClass(): string
    {
        return PropertyGroupDefinition::class;
    }

And override the administration component to also include this association when loading the entity:

Component.override('sw-property-detail', {
    methods: {
        loadEntityData() {
            this.isLoading = true;
            const criteria = this.defaultCriteria;
            criteria.addAssociation('customFieldSet', new Criteria(1, 500));

            this.propertyRepository.get(this.groupId, Shopware.Context.api, criteria)
                .then((currentGroup) => {
                    this.propertyGroup = currentGroup;
                    this.isLoading = false;
                }).catch(() => {
                this.isLoading = false;
            });
        }
    }
});

(I tried to override defaultCriteria but that didn't work because of this.$super being unable to access computed properties).

But it keeps saying FRAMEWORK__ASSOCIATION_NOT_FOUND. I debugged the EntityDefinition and it seems that this extension is not even loaded.

I checked if my EntityExtension is loaded in the Symfony container and it is, but it seems that it doesn't reach the entity definition.


Solution

  • The EntityExtension seems to be missing the addition of a FkField inside the function extendFields:

    public function extendFields(FieldCollection $collection): void
    {
        $collection->add(
            (new FkField('custom_field_set', 'customFieldSetId', CustomFieldSetDefinition::class)),
        );
        $collection->add(
            (new ManyToOneAssociationField('customFieldSet', 'custom_field_set', CustomFieldSetDefinition::class))
        );
    }
    

    A new use statement has to be added for the FkField:

    use Shopware\Core\Framework\DataAbstractionLayer\Field\FkField;