【发布时间】:2017-02-01 22:00:16
【问题描述】:
我正在阅读 Magento 2 教程,但在调用 create() 方法后,我无法从自定义模型的工厂获取集合。它抛出一个错误,说“模型集合资源名称未定义”。我已经清除了 /var/generation 并重新编译了 di。
公司/模块/型号/Vendor.php
namespace Company\Module\Model;
class Vendor extends \Magento\Framework\Model\AbstractModel {
protected function _constructor() {
$this->_init('Company\Module\Model\Resource\Vendor');
}
}
公司/模块/模型/资源/Vendor.php
namespace Company\Module\Model\Resource;
class Vendor extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
protected function _construct()
{
$this->_init(
'company_vendor',
'vendor_id'
);
}
}
公司/模块/模型/资源/供应商/Collection.php
namespace Company\Module\Model\Resource\Vendor;
class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
protected function _construct()
{
$this->_init(
'Company\Module\Model\Vendor',
'Company\Module\Model\Resource\Vendor'
);
}
}
公司/模块/区块/VendorList.php
namespace Company\Module\Block;
class VendorList extends \Magento\Framework\View\Element\Template {
protected $vendorFactory;
public function __construct(\Magento\Framework\View\Element\Template\Context $context,
\Company\Module\Model\VendorFactory $vendorFactory,
array $data = [])
{
parent::__construct($context, $data);
$this->vendorFactory = $vendorFactory;
}
public function getVendors() {
return $this->vendorFactory->create()->getCollection()->getItems(); //fails on getCollection()
}
这是我得到的错误:
1 个例外: 异常 #0 (Magento\Framework\Exception\LocalizedException):模型集合资源名称未定义。
【问题讨论】: