php 如何在订阅服务器中获取自定义实体关联字段?'Map定义既没有实体也没有集合'错误

y0u0uwnf  于 2023-08-02  发布在  PHP
关注(0)|答案(1)|浏览(139)

我将在订阅服务器中使用自定义实体关联字段。但我得到这个错误:第一个月
Subscriber.php:

class Subscriber implements EventSubscriberInterface
{
    private EntityRepository $myfilter_property_group_option_tagRepository;

    public function __construct(EntityRepository $myfilter_property_group_option_tagRepository)
    {
        $this->myfilter_property_group_option_tagRepository = $myfilter_property_group_option_tagRepository;
    }

    public static function getSubscribedEvents(): array
    {
        return [
            ProductListingCriteriaEvent::class => [
                ['onListingCriteria', -200],
            ],
        ];
    }

    public function onListingCriteria(ProductListingCriteriaEvent $event): void
    {
        $context = $event->getContext();
        $optiontags = $this->myfilter_property_group_option_tagRepository->search(new Criteria(), $context);
        $event->getContext()->addExtension('optionTags', new ArrayEntity($optiontags));
        
        // Further 
        $event->getCriteria()->addAssociation('properties');
        $event->getCriteria()->addAssociation('properties.group');

        $criteria = $event->getCriteria();
        $filters = $criteria->getExtension('filters');

        if (!$filters instanceof FilterCollection) {
            return;
        }

        $propertyFilter = $filters->get('properties');
        // More

字符串
services.xml:

<service id="MyFilter\Subscriber\Subscriber">
    <tag name="kernel.event_subscriber"/>
    <argument type="service" id="myfilter_property_group_option_tag.repository"/>
</service>


有人知道我做错了什么吗?
此外,在管理组件中已创建此实体的扩展。可以用那个吗?

7qhs6swi

7qhs6swi1#

myfilter_property_group_option_tag基于Map定义,推测Mapproperty_group_optiontag实体。Map本身无法获取。改为获取Map两侧的实体。

$criteria = new Criteria();
$criteria->addAssociation('myTags');

$tags = new TagCollection();
$options = $this->optionRepository->search($criteria, $context->getContext())->getEntities();
/** @var PropertyGroupOptionEntity $option */
foreach ($options as $option) {
    $extension = $option->getExtension('myTags');
    
    if (!$extension instanceof TagCollection) {
        continue;
    }
    
    $tags->merge($extension);
}

字符串

相关问题