我正试图创建一个Magento 2模块,用一些额外的逻辑来扩展默认的Customer::loadByEmail()
类和方法。
由于我的模块包含一些不同的类/文件,我创建了一个包含代码的公共要点,而不是用大量代码污染这篇文章。
完整代码:https://gist.github.com/JasonMortonNZ/90ada76ad5511a37d2c6
此外,所有代码都位于文件夹project-root/app/code/Jason/OCUsers
中,以供参考。
工作原理:
- 当我从命令行运行
magento module:status
命令时,模块被识别为我的Magento - 我可以成功地启用和禁用该模块,尽管迁移(架构升级)似乎没有运行
什么是不工作:
- 安装和升级时的架构更新似乎不起作用。没有数据库架构更新持续存在或生效
- DI似乎不正确,因为我创建的新
Customer
类和loadByEmail
方法没有被命中
关于为什么会出现这两个问题的任何帮助或建议都将不胜感激:)
module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Jason_OCUser" setup_version="2.0.0"/>
<sequence>
<module name="Magento_Customer"/>
</sequence>
</config>
di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento'Customer'Api'Data'CustomerInterface" type="Jason'OCUser'Model'Customer" />
<type name="Magento'Framework'Model'ActionValidator'RemoveAction">
<arguments>
<argument name="protectedModels" xsi:type="array">
<item name="customer" xsi:type="string">Jason'OCUser'Model'Customer</item>
</argument>
</arguments>
</type>
</config>
Customer.php
<?php
namespace Jason'OCUser'Model;
use Magento'Customer'Model'Customer as MCustomer;
class Customer extends MCustomer
{
/**
* Load customer by email
*
* @param string $customerEmail
* @return $this
*/
public function loadByEmail($customerEmail)
{
die('Not reaching this :( ');
}
}
InstallSchema.php
<?php
namespace Jason'OCUser'Setup;
use Magento'Framework'DB'Ddl'Table;
use Magento'Framework'Setup'ModuleContextInterface;
use Magento'Framework'Setup'SchemaSetupInterface;
use Magento'Framework'Setup'InstallSchemaInterface;
class InstallSchema implements InstallSchemaInterface
{
/**
* Installs DB schema for a module
*
* @param SchemaSetupInterface $setup
* @param ModuleContextInterface $context
* @return void
*/
public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
$installer = $setup;
$installer->startSetup();
/**
* Add salt column and oc user status
*/
$installer->getConnection()->addColumn(
'customer_entity',
'oc_salt',
[
'type' => Table::TYPE_TEXT,
'nullable' => true,
'default' => null,
'length' => 9,
'comment' => ''
]
);
$installer->getConnection()->addColumn(
'customer_entity',
'oc_user',
[
'type' => Table::TYPE_BOOLEAN,
'nullable' => false,
'default' => 0,
'comment' => ''
]
);
$installer->endSetup();
}
}
您的di.xml
似乎不正确。请参见preference
标记上的for
属性。见下文,我已将Magento'Customer'Api'Data'CustomerInterface
替换为Magento'Customer'Model'Customer
。
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento'Customer'Model'Customer" type="Jason'OCUser'Model'Customer" />
<type name="Magento'Framework'Model'ActionValidator'RemoveAction">
<arguments>
<argument name="protectedModels" xsi:type="array">
<item name="customer" xsi:type="string">Jason'OCUser'Model'Customer</item>
</argument>
</arguments>
</type>
</config>