关联在数据库中,但无法通过DAL检索将检索空的关联数组

问题描述

我正在关注高级开发者教程(Automatically bind the challenge to a button)。

目前我处于第7步,根据本教程,我到目前为止所做的工作应该可以正常工作。

但事实并非如此。

数据库显示了关联,但是我无法从存储库中检索它们。

解决方法

您必须将关联添加到条件中。

$criteria->addAssociation("name_of_association")
没有它,关联就为空。

,

好吧,事实证明,我偶然打开了两个参数。当我正确设置它们时,它应该可以正常工作。

<?php declare(strict_types=1);

namespace Swag\BundleExample\Core\Content\Product;

use Shopware\Core\Content\Product\ProductDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\EntityExtension;
use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\Inherited;
use Shopware\Core\Framework\DataAbstractionLayer\Field\ManyToManyAssociationField;
use Shopware\Core\Framework\DataAbstractionLayer\FieldCollection;
use Swag\BundleExample\Core\Content\Bundle\Aggregate\BundleProduct\BundleProductDefinition;
use Swag\BundleExample\Core\Content\Bundle\BundleDefinition;

class ProductExtension extends EntityExtension
{
    public function extendFields(FieldCollection $collection): void
    {
        $collection->add(
            (new ManyToManyAssociationField(
                'bundles',BundleDefinition::class,BundleProductDefinition::class,'product_id','bundle_id'
            ))->addFlags(new Inherited())
        );
    }

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

我说的是'product_id'和'bundle_id'。就我而言,我将“ product_id”作为最后一个参数。