As type string:(but I need as an object)
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
<extension_attributes for="MagentoSalesApiDataOrderInterface">
<attribute code="admin_user" type="string"/>
</extension_attributes>
</config>
should be something like:
<attribute code="admin_user" type="pathtoobject"/>
Plugin:
<?php
declare(strict_types=1);
namespace CollinsOrderTrackingPlugin;
use MagentoSalesApiDataOrderInterface;
use MagentoSalesApiDataOrderSearchResultInterface;
use MagentoSalesApiOrderRepositoryInterface;
use CollinsOrderTrackingModelAttributesFactory;
use MagentoBackendModelAuthSession;
use CollinsOrderTrackingModelResourceModelAttributeCollectionFactory;
use CollinsOrderTrackingApiAttributesRepositoryInterface;
use MagentoFrameworkApiSearchCriteriaBuilder;
use MagentoFrameworkSessionSessionManagerInterface;
class OrderRepositoryPlugin
{
/**
* @var AttributesFactory
*/
private $attributesFactory;
/**
* @var Session
*/
private $session;
/**
* @var CollectionFactory
*/
private $collectionFactory;
/**
* @var AttributesRepositoryInterface
*/
private $attributesRepository;
/**
* @var SearchCriteriaBuilder
*/
private $criteriaBuilder;
/**
* @param AttributesFactory $attributesFactory
* @param Session $session
* @param CollectionFactory $collectionFactory
* @param AttributesRepositoryInterface $attributesRepository
* @param SearchCriteriaBuilder $criteriaBuilder
*/
public function __construct(
AttributesFactory $attributesFactory,
Session $session,
CollectionFactory $collectionFactory,
AttributesRepositoryInterface $attributesRepository,
SearchCriteriaBuilder $criteriaBuilder
// SessionManagerInterface $coreSession
) {
$this->attributesFactory = $attributesFactory;
$this->session = $session;
$this->collectionFactory = $collectionFactory;
$this->attributesRepository = $attributesRepository;
$this->criteriaBuilder = $criteriaBuilder;
// $this->sessionManager = $coreSession;
}
/**
* @param OrderRepositoryInterface $subject
* @param OrderSearchResultInterface $orderSearchResult
* @return OrderSearchResultInterface
*/
public function afterGetList(
OrderRepositoryInterface $subject,
OrderSearchResultInterface $orderSearchResult
): OrderSearchResultInterface {
return $orderSearchResult;
}
/**
* @param OrderRepositoryInterface $subject
* @param OrderInterface $order
* @return OrderInterface
*/
public function afterGet(
OrderRepositoryInterface $subject,
OrderInterface $order
): OrderInterface {
$key = 'collins_ordertracking_order'.$order->getEntityId();
$adminUserId = null;
$searchCritertia = $this->criteriaBuilder->addFilter('order_id',$order->getEntityId(),'eq');
$sessionData = $this->session->getData($key);
$entityId = $order->getEntityId();
// GetList must be loaded more than once when using session.
// Functionality below ensures that get list is ran twice but not more.
if(!isset($sessionData)) {
$this->session->setData($key,$entityId);
$sessionData = $this->session->getData($key);
}
if($sessionData < ($entityId +2)) {
$this->session->setData($key,intval($sessionData) +1);
$repository = $this->attributesRepository->getList($searchCritertia->create());
foreach ($repository->getItems() as $item) {
$adminUserId = $item->getUserId();
}
if ($adminUserId != null) {
$extensionAttributes = $order->getExtensionAttributes();
$extensionAttributes->setAdminUser($adminUserId);
}
}
return $order;
}
}
I am trying to load the attributes in a plugin, but afterGet() loads the attributes many times. As you see, I am using session to limit the calls to 2, but that is a very bad practice. To my understanding, if I attach the object as the extension attribute oppose to using type string, then there would only be just 1 call to the database. How do I make this happen?