php 获取Shopware 6集合元素的值

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

我试着在几天内获取一个Collection类的元素的“名称”数组。我尝试了在Shopware 6中使用的所有可能的方法,但仍然无法在Subscriber中获得它:

public function onListingCriteria(ProductListingCriteriaEvent $event): void
    {
        $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');

        if (!$propertyFilter instanceof Filter || !\is_array($propertyFilter->getValues())) {
            return;
        }

        $currentPropertyOptions = $propertyFilter->getValues();

        $event->getContext()->addExtension('currentPropertyOptionsCriteria', new ArrayEntity($currentPropertyOptions));

        $criteria1 = new Criteria();
        $criteria1->addAssociation('Tags');
        $criteria1->setIds($currentPropertyOption);

        $tags = new TagCollection();
        $options = $this->optionRepository->search($criteria1, $event->getContext())->getEntities();

        foreach ($options as $option) {
            $extension = $option->getExtension('myTags');
            
            if (!$extension instanceof TagCollection) {
                continue;
            }
            
            $tags->merge($extension);
        }

        $tagnames = $tags->getElements('myTags');
        // doesn't work $tagnames = $options->getExtension('myTags')->get('names');
        // doesn't work $tagnames = $options->getExtension('myTags')->getValues();
        // etc..

        // test $event->getContext()->addExtension('myTags1', $tags);
        // test $event->getContext()->addExtension('myTagsNames1', new ArrayEntity($tagnames));

字符串
这里是myTags1扩展转储:

Shopware\Core\System\Tag\TagCollection {#8104 ▼
  #elements: array:10 [▼
    "49aa54d94e394bc2affb5b702d402e42" => Shopware\Core\System\Tag\TagEntity {#12939 ▶}
    "551fcda8a25f47c990d7a795ef251508" => Shopware\Core\System\Tag\TagEntity {#12943 ▶}
    "8c526404df194a88bf72073fea2fdabd" => Shopware\Core\System\Tag\TagEntity {#12947 ▼
      #name: "Name1"


有谁知道,如何得到一个数组的名字也许?

ac1kyiln

ac1kyiln1#

foreach ($options as $option) {
    $extension = $option->getExtension('myTags');
    
    if (!$extension instanceof TagCollection) {
        continue;
    }
    
    $tags->merge($extension);
}

$names = $tags->map(fn (TagEntity $tag) => $tag->getName());

字符串

相关问题