首页 文章

Magento 2错误 - 未定义模型集合资源名称

提问于
浏览
1

我正在阅读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):未定义模型集合资源名称 .

2 回答

  • 0

    您需要进行以下更改 .

    公司/模块/型号/ 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 /供应商/ 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'
            );
        }
    }
    
  • 0

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

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

相关问题