在 vendor/magento/framework/Model/AbstractModel.php 中调用成员函数 dispatch()
Posted
技术标签:
【中文标题】在 vendor/magento/framework/Model/AbstractModel.php 中调用成员函数 dispatch()【英文标题】:Call to a member function dispatch() on null in vendor/magento/framework/Model/AbstractModel.php 【发布时间】:2022-01-07 15:38:02 【问题描述】:我想在第一次安装模块时插入一些数据。
这是我的文件夹结构
Navien/Custom
-- Setup/InstallData.php
-- Model/
-- StateData.php
-- StateMaster.php
-- ResourceModel/
-- StateMaster.php
-- StateMaster/
-- Collection.php
这是我的脚本内容InstallData.php
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Eav\Model\Config;
use Navien\Custom\Model\StateData;
class InstallData implements InstallDataInterface
private $eavSetupFactory;
private $eavConfig;
private $stateDataMaster;
public function __construct(EavSetupFactory $eavSetupFactory, Config $eavConfig, StateData $stateData)
$this->eavSetupFactory = $eavSetupFactory;
$this->eavConfig = $eavConfig;
$this->stateDataMaster = $stateData;
public function install(
ModuleDataSetupInterface $setup,
ModuleContextInterface $context
)
$data = [
['code' => 'AL', 'name' => 'Alabama', 'abbreviation' => 'Ala.', 'country' => 'US'],
['code' => 'AK', 'name' => 'Alaska', 'abbreviation' => 'Alaska', 'country' => 'US']
];
$this->stateDataMaster->insertStates($data);
Model/StateData.php
namespace Navien\Custom\Model;
use Navien\Custom\Model\ResourceModel\StateMaster;
use Psr\Log\LoggerInterface;
use Magento\Framework\View\Element\Template\Context;
// class StateData extends \Magento\Framework\View\Element\Template
class StateData
protected $_logger;
protected $_stateMaster;
protected $_objectManager;
public function __construct(
LoggerInterface $logger,
StateMaster $stateMaster,
Context $context,
array $data = []
)
// parent::__construct($context, $data);
$this->_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$this->_logger = $logger;
public function insertStates($states)
if( !empty( $states ) )
$stateMasterRepository = $this->_objectManager->get('Navien\Custom\Model\StateMaster');
$stateData = $stateMasterRepository->getCollection()->getData();
foreach( $states as $key => $state )
if( (array_search($state['code'], array_column($stateData, 'code')) === FALSE) )
$stateMaster = $this->_objectManager->get('Navien\Custom\Model\StateMaster');
$stateMaster->setData( $state );
$stateMaster->save();
public function execute()
Model/StateMaster.php
namespace Navien\Custom\Model;
use \Magento\Framework\Model\AbstractModel;
use \Psr\Log\LoggerInterface;
class StateMaster extends AbstractModel
protected $_logger;
public function __construct(
LoggerInterface $logger
)
$this->_init('Navien\Custom\Model\ResourceModel\StateMaster');
Model/ResourceModel/StateMaster.php
namespace Navien\Custom\Model\ResourceModel;
use \Psr\Log\LoggerInterface;
class StateMaster extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
protected function _construct()
$this->_init('navien_states','ID');
Navien\Custom\Model\ResourceModel\StateMaster
namespace Navien\Custom\Model\ResourceModel\StateMaster;
class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
public function __construct(
\Magento\Framework\Data\Collection\EntityFactory $entityFactory,
\Psr\Log\LoggerInterface $logger,
\Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
\Magento\Framework\Event\ManagerInterface $eventManager,
\Magento\Catalog\Model\ResourceModel\Product\Option\Value\CollectionFactory $optionValueCollectionFactory,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
\Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
)
$this->_optionValueCollectionFactory = $optionValueCollectionFactory;
$this->_storeManager = $storeManager;
parent::__construct(
$entityFactory,
$logger,
$fetchStrategy,
$eventManager,
$connection,
$resource
);
$this->logger = $logger;
protected function _construct()
$this->_init('Navien\Custom\Model\StateMaster','Navien\Custom\Model\ResourceModel\StateMaster');
现在我收到此错误
Error: Call to a member function dispatch() on null in /var/www/dev/vendor/magento/framework/Model/AbstractModel.php:701
Stack trace: #0 /var/www/dev/vendor/magento/framework/Model/ResourceModel/Db/AbstractDb.php(412): Magento\Framework\Model\AbstractModel->beforeSave()
#1 /var/www/dev/vendor/magento/framework/Model/AbstractModel.php(655): Magento\Framework\Model\ResourceModel\Db\AbstractDb->save()
#2 /var/www/dev/app/code/Navien/Custom/Model/StateData.php(62): Magento\Framework\Model\AbstractModel->save()
【问题讨论】:
【参考方案1】:这是因为你的构造函数与父块构造函数不匹配。
模型/StateMaster.php
namespace Navien\Custom\Model;
use \Magento\Framework\Model\AbstractModel;
StateMaster 类扩展 AbstractModel
public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = []
)
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
protected function _construct()
$this->_init(\Navien\Custom\Model\ResourceModel\StateMaster::class);
【讨论】:
以上是关于在 vendor/magento/framework/Model/AbstractModel.php 中调用成员函数 dispatch()的主要内容,如果未能解决你的问题,请参考以下文章
在 React 应用程序中在哪里转换数据 - 在 Express 中还是在前端使用 React?