我正在尝试关注Alan Storm的教程here。 当我尝试插入或检索单个记录时,代码工作正常,但是当我使用集合时它会破裂。我按照他在教程中说明的所有步骤,这让我发疯,因为只有收藏不起作用!!
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;
}
这会出现以下错误:
Vendor \ Mymodule \ Model \ Configurations不扩展\ Magento \ Framework \ DataObject
配置模型的代码如下
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');
}
}
集合
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');
}
}
任何提示都会非常感激。 感谢