我正在创建一个API方法,我要将自定义JSON项的数组发送到该方法。我不想在JSON对象后面建立数据库模型。 JSON只是将瞬态数据发送到我的API方法的一种方式,并且没有任何数据库表直接支持。
这是我要发布的JSON数据:
{
"stockItems": [
{
"sku": "SKU-1234",
"qty": 15
},
{
"sku": "SKU-5678",
"qty": 25
}
]
}
这是我的自定义模型和界面:
StockItemInterface.php
namespace MyCompany\CatalogInventory\Api\Data;
/**
* @api
*/
interface StockItemInterface
{
/**
* @return float
*/
public function getQty();
/**
* @return string
*/
public function getSku();
/**
* @param float $qty
* @return $this
*/
public function setQty($qty);
/**
* @param string $sku
* @return $this
*/
public function setSku($sku);
}
Item.php
namespace MyCompany\CatalogInventory\Model\Stock;
use MyCompany\CatalogInventory\Api\Data\StockItemInterface;
class Item implements StockItemInterface
{
protected $sku;
protected $qty;
/**
* @param array $data
*/
public function __construct(array $data = [])
{
}
/**
* @return float
*/
public function getQty()
{
return $this->qty;
}
/**
* @return string
*/
public function getSku()
{
return $this->sku;
}
/**
* @param float $qty
* @return $this
*/
public function setQty($qty)
{
$this->qty = $qty;
}
/**
* @param string $sku
* @return $this
*/
public function setSku($sku)
{
$this->sku = $sku;
}
}
这是我的API方法
StockRegistryInterface.php
namespace MyCompany\CatalogInventory\Api;
/**
* Interface StockRegistryInterface
* @api
*/
interface StockRegistryInterface
{
/**
* @param \MyCompany\CatalogInventory\Api\Data\StockItemInterface[] $stockItems
* @return int
*/
public function updateStockItems(array $stockItems);
}
股票登记处
namespace MyCompany\CatalogInventory\Model;
use Magento\Catalog\Model\ProductFactory;
use Magento\CatalogInventory\Api\Data\StockItemInterface;
use Magento\CatalogInventory\Api\StockConfigurationInterface;
use Magento\CatalogInventory\Api\StockItemCriteriaInterfaceFactory;
use Magento\CatalogInventory\Api\StockItemRepositoryInterface;
use Magento\CatalogInventory\Model\Spi\StockRegistryProviderInterface;
use MyCompany\CatalogInventory\Api\StockRegistryInterface;
/**
* Class StockRegistry
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class StockRegistry extends \Magento\CatalogInventory\Model\StockRegistry implements StockRegistryInterface
{
/**
* @param StockConfigurationInterface $stockConfiguration
* @param StockRegistryProviderInterface $stockRegistryProvider
* @param StockItemRepositoryInterface $stockItemRepository
* @param StockItemCriteriaInterfaceFactory $criteriaFactory
* @param ProductFactory $productFactory
*/
public function __construct(
StockConfigurationInterface $stockConfiguration,
StockRegistryProviderInterface $stockRegistryProvider,
StockItemRepositoryInterface $stockItemRepository,
StockItemCriteriaInterfaceFactory $criteriaFactory,
ProductFactory $productFactory
) {
parent::__construct($stockConfiguration, $stockRegistryProvider, $stockItemRepository, $criteriaFactory, $productFactory);
}
/**
* @inheritdoc
*/
public function updateStockItems(array $stockItems)
{
// do stuff
}
}
发布到API方法时,出现此错误
"Fatal Error: 'Uncaught Error: Cannot instantiate interface MyCompany\\CatalogInventory\\Api\\Data\\StockItemInterface in
\/var\/www\/magento2\/lib\/internal\/Magento\/Framework\/ObjectManager\/Factory\/AbstractFactory.php:111
Stack trace:
#0 \/var\/www\/magento2\/lib\/internal\/Magento\/Framework\/ObjectManager\/Factory\/Compiled.php(108):
Magento\\Framework\\ObjectManager\\Factory\\AbstractFactory->createObject('MyCompany\\\\CatalogI...', Array)
#1 \/var\/www\/magento2\/lib\/internal\/Magento\/Framework\/ObjectManager\/ObjectManager.php(56):
Magento\\Framework\\ObjectManager\\Factory\\Compiled->create('MyCompany\\\\CatalogI...', Array)
#2 \/var\/www\/magento2\/lib\/internal\/Magento\/Framework\/Webapi\/ServiceInputProcessor.php(166):
Magento\\Framework\\ObjectManager\\ObjectManager->create('\\\\MyCompany\\\\Catalog...')
#3 \/var\/www\/magento2\/lib\/internal\/Magento\/Framework\/Webapi\/ServiceInputProcessor.php(399):
Magento\\Framework\\Webapi\\ServiceInputProcessor->_createFromArray('\\\\MyCompany\\\\Catalog...', Array)
#4 \/var\/www\/magento2\/lib\/internal\/Magento\/Framework\/Webapi\/ServiceInputProcessor.php(332):
' in '\/var\/www\/magento2\/lib\/internal\/Magento\/Framework\/ObjectManager\/Factory\/AbstractFactory.php' on line 111"
在AbstractFactory.createObject
中发生了异常:
protected function createObject($type, $args)
{
return new $type(...array_values($args));
}
问题在于$args
是一个空数组,因此array_values
抛出异常。
如何编写Item
类,以便Magento可以成功构建它,而无需支持数据库模型?