Shopware 6: Unexcpected behavior on custom product extension

Viewed 51

what exactly is the purpose or the expected behavior of the autoload parameter in CustomExtension->extendFields() OneToOneAssociation of a custom extension?

As i understand, with true the extension is always loaded, even if no has record, maybe as null

Expected

// debug source
...
product: Proxy
  [[Handler]]: Object
  [[Target]]: Object
    ...
    extensions:
      my_custom_extension: null

Actual

 // debug source
...
product: Proxy
  [[Handler]]: Object
  [[Target]]: Object
    ...
    extensions: {}

EDIT

// ProductFactoryProductExtension.php
<?php declare(strict_types=1);

namespace ItR22ProductFactory\Extension\ProductFactoryProduct;

use Shopware\Core\Content\Product\ProductDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\EntityExtension;
use Shopware\Core\Framework\DataAbstractionLayer\Field\OneToOneAssociationField;
use Shopware\Core\Framework\DataAbstractionLayer\FieldCollection;

class ProductFactoryProductExtension extends EntityExtension
{

    public function extendFields(FieldCollection $collection): void
    {
        $collection->add(
            new OneToOneAssociationField(
                'productFactoryProduct',
                'id',
                'product_id',
                ProductFactoryProductExtensionDefinition::class,
                true
            )
        );
    }

    public function getDefinitionClass(): string
    {
        return ProductDefinition::class;
    }
}
// ProductFactoryProductExtensionDefinition.php
<?php declare(strict_types=1);

namespace ItR22ProductFactory\Extension\ProductFactoryProduct;

use Shopware\Core\Content\Product\ProductDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\EntityDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\Field\BoolField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\FkField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\PrimaryKey;
use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\Required;
use Shopware\Core\Framework\DataAbstractionLayer\Field\IdField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\JsonField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\OneToOneAssociationField;
use Shopware\Core\Framework\DataAbstractionLayer\FieldCollection;

class ProductFactoryProductExtensionDefinition extends EntityDefinition
{
    public const ENTITY_NAME = 'product_factory_product';

    public function getEntityName(): string
    {
        return self::ENTITY_NAME;
    }

    public function getEntityClass(): string
    {
        return ProductFactoryProductEntity::class;
    }

    protected function defineFields(): FieldCollection
    {
        return new FieldCollection([
            (new IdField('id', 'id'))->addFlags(new Required(), new PrimaryKey()),
            new FkField('product_id', 'productId', ProductDefinition::class),
            (new BoolField('combinable', 'combinable')),
            (new JsonField('combinations', 'combinations')),
            new OneToOneAssociationField(
                'product',
                'product_id',
                'id',
                ProductDefinition::class,
                false
            )
        ]);
    }
}

EDIT #2

// sw-product-detail/index.js
...
Component.override('sw-product-detail', {
  ...
  
  /**
   *
   */
   loadProduct() {
      this.productCriteria.addAssociation('productFactoryProduct');
      this.$super('loadProduct');
   },
   ...
}
1 Answers

If you want data associated to an entity be fetched as well, in most cases the association needs to be actively added by calling Criteria::addAssociation. By setting autoload of the association field to true, the association is always added internally. Note that for OneToOneAssociation the default value of autoload is true.

Related