dt614037527
dt614037527
2016-10-11 18:22

不能在Magento2中使用带有自定义表的getCollection

I'm trying to follow Alan Storm's tutorial here. The code works fine when I try to insert or retrieve a single record, but it cracks when I use collections. I followed all steps he illustrated in the tutorial, which drives me crazy because only collections wouldn't work!!

public function __construct(
\Magento\Backend\App\Action\Context $context,
\Vendor\Mymodule\Model\ConfigurationsFactory $myClass
) {
    $this->myClass = $myClass;
    parent::__construct($context);
}

public function execute() {
 $todo = $this->myClass->create();

 $collection = $todo->getCollection();

 foreach($collection as $item)
  {
    var_dump('Item ID: ' . $item->getConfigId());
    var_dump($item->getData());
  }
exit;

}

This gives the following error:

Vendor\Mymodule\Model\Configurations does not extend \Magento\Framework\DataObject

The code for configurations model is as below

namespace Vendor\Mymodule\Model;
use Magento\Framework\Model\AbstractModel;

class Configurations extends AbstractModel
{
    protected function _construct()
    {
        $this->_init('Vendor\Mymodule\Model\ResourceModel\Configurations');
    }
}

ResourceModel

namespace Vendor\Mymodule\Model\ResourceModel;
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;

class Configurations extends AbstractDb
{
    protected function _construct()
    {
        $this->_init('vendor_configurations','config_id');
    }
}

Collection

namespace Vendor\Mymodule\Model\ResourceModel\Configurations;

class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
    protected function _construct()
    {
        $this->_init('
             Vendor\Mymodule\Model\Configurations',
            'Vendor\Mymodule\Model\ResourceModel\Configurations');
    }
}

any hints would be really appreciated. Thanks

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答