1

我正在尝试创建一个 Magento 2 模块以Customer::loadByEmail()使用一些额外的逻辑来扩展默认类和方法。

由于我的模块包含几个不同的类/文件,我创建了一个包含代码的公共要点,而不是用大量代码污染这篇文章。

完整代码:https ://gist.github.com/JasonMortonNZ/90ada76ad5511a37d2c6

同样作为参考,所有代码都位于文件夹中project-root/app/code/Jason/OCUsers

什么工作:

  • magento module:status当我从命令行运行命令时,该模块被我的 Magento 识别。
  • 尽管迁移(架构升级)似乎没有运行,但我可以成功启用和禁用该模块。

什么不起作用

  • 安装和升级的架构更新似乎不起作用。没有数据库架构更新持续存在或生效。
  • 看起来 DI 不正确,因为我创建的新Customer类和loadByEmail方法没有被击中。

任何有关为什么会出现这两个问题的帮助或建议将不胜感激:)

模块.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>

客户.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 :( ');
    }
}

安装Schema.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();
    }
}
4

1 回答 1

0

你的di.xml似乎是不正确的。查看标签for上的属性。preference请参阅下面我已替换Magento\Customer\Api\Data\CustomerInterfaceMagento\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>
于 2015-11-30T22:07:15.120 回答