Magento2 CE не может использовать атрибуты пользовательского расширения модели - PullRequest
2 голосов
/ 07 мая 2020

Я пытаюсь изучить Magento 2 (версия сообщества v.2.3.4), но, похоже, я наткнулся на кирпичную стену при работе с атрибутами расширения. Я сделал все в соответствии с документацией https://devdocs.magento.com/guides/v2.3/extension-dev-guide/extension_attributes/adding-attributes.html и https://github.com/magento/magento2-samples/tree/master/sample-external-links, но сгенерированный кодом компилятора magento (имя класса «AffiliateMemberExtension») не имеет метода добавления моего настраиваемого атрибута названный «образец».

Точное сообщение об ошибке, которое я получаю от конечной точки REST:

Fatal Error: 'Uncaught Error: Call to undefined method SimplifiedMagento\\Database\\Api\\Data\\AffiliateMemberExtension::setSample() in \/home\/azumi\/work\/magento2\/app\/code\/SimplifiedMagento\/Attribute\/Model\/Plugin\/CodeAttributeExtension.php:37

Мой полный исходный код находится здесь: https://gitlab.com/naevus19/my-magento/-/tree/master/app/code/SimplifiedMagento/Attribute

Любая помощь будет принята с благодарностью. Я не знаю, как отлаживать это дальше: (

extension_attributes. xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="\SimplifiedMagento\Database\Api\Data\AffiliateMemberInterface">
        <attribute code="sample" type="string" />
    </extension_attributes>
</config>

di. xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <type name="\SimplifiedMagento\Database\Api\AffiliateMemberRepositoryInterface">
        <plugin name="attribute_code_plugin" type="\SimplifiedMagento\Attribute\Model\Plugin\CodeAttributeExtension" />
    </type>
</config>

модуль. xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="SimplifiedMagento_Attribute" setup_version="0.1.0"  />
</config>

код плагина CodeAttributeExtension. php

<?php
/**
 * Created by PhpStorm.
 * User: kamil
 * Date: 07.05.20
 * Time: 16:39
 */

namespace SimplifiedMagento\Attribute\Model\Plugin;

use SimplifiedMagento\Database\Api\AffiliateMemberRepositoryInterface;
use \SimplifiedMagento\Database\Api\Data\AffiliateMemberExtensionFactory;
use SimplifiedMagento\Database\Api\Data\AffiliateMemberInterface;
use SimplifiedMagento\Database\Model\AffiliateMember;
use \SimplifiedMagento\Database\Model\AffiliateMemberRepository;

class CodeAttributeExtension
{
    protected $extensionFactory;

    public function __construct(AffiliateMemberExtensionFactory $extensionFactory)
    {
        $this->extensionFactory = $extensionFactory;
    }

    public function afterGetAffiliateMemberById
    (
        AffiliateMemberRepositoryInterface $subject,
        AffiliateMember $entity
    ) {
        $entity->setCustomAttribute('sample', "Code #" . $entity->getId());
        $extensionAttributes = $entity->getExtensionAttributes();

        if (empty($extensionAttributes)) {
            $extensionAttributes = $this->extensionFactory->create();
        }
        $extensionAttributes->setSample("Code #" . $entity->getId());
        $entity->setExtensionAttributes($extensionAttributes);

        return $entity;
    }

}

1 Ответ

0 голосов
/ 11 мая 2020

В первую очередь, я попытаюсь установить атрибут расширения с помощью метода setData(), например:

$extensionAttributes->setData('sample', 'Code #' . $entity->getId());

и удалить первую backsla sh \SimplifiedMagento в ваших *. xml файлах, где вы объявили пространства имен ваших классов и интерфейсов.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...