微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在Model Magento中为GraphQL选择数据

如何解决在Model Magento中为GraphQL选择数据

我创建了一个模型来搜索magento中的Consumer属性,但是选择不正确。 我想将值插入graphql数组。 下面的代码对于显示根据用户ID的属性值是必需的,但是只返回null。

model / attribute.PHP

<?PHP

namespace Enterprise\GraphQL\Model;

use Magento\Cron\Exception;
use Magento\Framework\Model\AbstractModel;

/**
 * Contact Model
 *
 * @author      Pierre FAY
 */
class Attribute extends AbstractModel
{
    /**
     * @var \Magento\Framework\Stdlib\DateTime
     */
    protected $_dateTime;

    /**
     * @return void
     */
    protected function _construct()
    {
        $this->_init(\Enterprise\Attribute\Model\ResourceModel\Attribute::class);
    }
    
}

model / ResourceModel / Attribute.PHP

<?PHP

namespace Enterprise\GraphQL\Model\ResourceModel;

use Magento\Framework\Model\ResourceModel\Db\AbstractDb;

class Attribute extends AbstractDb
{
    public function _construct()
    {
        $this->_init('customer_entity_varchar','value_id');
    }
}

解析器/客户

namespace Enterprise\GraphQL\Model\Resolver;

use Magento\CustomergraphQl\Model\Customer\GetCustomer;
use Enterprise\GraphQl\Model\Attribute;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\CustomergraphQl\Model\Customer\ExtractCustomerData;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\GraphQl\Model\Query\ContextInterface;


class CustomerGarage implements ResolverInterface
{
    private $getCustomer;

    private $extractCustomerData;

    public function __construct(
        GetCustomer $getCustomer,ExtractCustomerData $extractCustomerData
    ) {
        $this->getCustomer = $getCustomer;
        $this->extractCustomerData = $extractCustomerData;
    }


    public function resolve(
        Field $field,$context,ResolveInfo $info,array $value = null,array $args = null
    ) {
        $attributeModel = $this->_objectManager->create('ObjectEdge\GraphQL\Model\Attribute');
        $collection = $attributeModel->getCollection();
            $valor = 0;
        foreach($collection as $attribute) {
            $valor = $attribute->getData();
        }   
        
        /** @var ContextInterface $context */
        if (false === $context->getExtensionAttributes()->getIsCustomer()) {
            throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
        }

        $customer = $this->getCustomer->execute($context);
        $data = $this->extractCustomerData->execute($customer);
        
        
        $model[] = array(
            "make" => "Teste","model" => $valor,"specification" => "1.0","year" => 2020,"isDefault" => "Teste"
        );
        
        return $model;
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。