【问题标题】:Magento 2 Error - Model collection resource name is not definedMagento 2 错误 - 模型集合资源名称未定义
【发布时间】: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):模型集合资源名称未定义。

【问题讨论】:

    标签: magento magento2


    【解决方案1】:

    您需要进行以下更改。

    公司/模块/型号/Vendor.php

    namespace Company\Module\Model;
    
    class Vendor extends \Magento\Framework\Model\AbstractModel {
        protected function _constructor() {
            $this->_init('Company\Module\Model\ResourceModel\Vendor');
        }
    }
    

    公司/模块/模型/ResourceModel/Vendor.php

    namespace Company\Module\Model\ResourceModel;
    
    class Vendor extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
    {
        protected function _construct()
        {
            $this->_init('company_vendor','vendor_id');
        }
    }
    

    公司/模块/模型/ResourceModel/Vendor/Collection.php

    namespace Company\Module\Model\ResourceModel\Vendor;
    
    class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
    {
        public function _construct()
        {
            $this->_init('Company\Module\Model\Vendor','Company\Module\Model\ResourceModel\Vendor'
            );
        }
    }
    

    【讨论】:

    • 尝试缓存magento然后检查。
    【解决方案2】:

    问题是我有 _constructor() 而不是 _construct()

    namespace Company\Module\Model;
    
    class Vendor extends \Magento\Framework\Model\AbstractModel {
    
        protected function _construct() {
            $this->_init('Company\Module\Model\Resource\Vendor');
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-28
      • 1970-01-01
      相关资源
      最近更新 更多