微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Magento 2 数据补丁问题

如何解决Magento 2 数据补丁问题

我正在编写一个基本模块,它将为我们销售的物品创建物料清单。该模块包含三个表:氯化器、设备和氯化器_设备。

这些表由 db_schema.xml 生成,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
    <table name="chlorinators" resource="default" engine="innodb" comment="Chlorinators Table">
        <column xsi:type="smallint" name="chlorinator_id" unsigned="true" nullable="false" comment="Chlorinator ID"/>
        <column xsi:type="varchar" name="chlorinator_name" nullable="false" length="1024" comment="Chlorinator Name"/>
        <column xsi:type="boolean" name="has_pump" nullable="false" comment="Chlorinator has pump"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="chlorinator_id"/>
        </constraint>
    </table>
    <table name="equipment" resource="default" engine="innodb" comment="Equipment Table">
        <column xsi:type="smallint" name="equipment_id" unsigned="true" nullable="false" comment="Equipment ID"/>
        <column xsi:type="varchar" name="equipment_name" nullable="false" length="1024" comment="Equipment Name"/>
        <column xsi:type="varchar" name="cutsheet_url" nullable="false" default="" comment="Cutsheet URL - Complete"/>
        <column xsi:type="varchar" name="cutsheet_stub" nullable="false" comment="Cutsheet URL - Stub"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="equipment_id"/>
        </constraint>
    </table>
    <table name="chlorinator_equipment" resource="default" engine="innodb" comment="Chlorinator Equipment Relation Table">
        <column xsi:type="smallint" name="id" unsigned="true" nullable="false" identity="true" comment="Chlorinator Equipment ID"/>
        <column xsi:type="smallint" name="equipment_id" unsigned="true" nullable="false" comment="Equipment ID"/>
        <column xsi:type="smallint" name="chlorinator_id" unsigned="true" nullable="false" comment="Chlorinator ID"/>
        <column xsi:type='tinyint' name='quantity' unsigned='true' nullable='false' comment='Quantity' />
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="id"/>
        </constraint>
        <constraint xsi:type='foreign' referenceId="FK_CHLOR_ID" table='chlorinator_equipment' column='chlorinator_id' referenceTable='chlorinators' referenceColumn='chlorinator_id' onDelete='CASCADE'/>
        <constraint xsi:type='foreign' referenceId="FK_EQUIP_ID" table='chlorinator_equipment' column='equipment_id' referenceTable='equipment' referenceColumn='equipment_id' onDelete='CASCADE'/>
    </table>
</schema>

现在我已经创建了表,我希望使用 Magento 的声明式架构将数据输入到表中。就这个问题而言,我只关心将数据输入到“加氯器”表中。

我为加氯器创建了一个模型 (\Jared\Submittal\Model\Chlorinator.PHP):

<?PHP

namespace Jared\Submittal\Model;

class Chlorinator extends \Magento\Framework\Model\AbstractModel
{
    protected function _construct()
    {
        $this->_init('Jared\Submittal\Model\ResourceModel\Chlorinator');
    }
}

还有一个 ResourceModel (\Jared\Submittal\Model\ResourceModel\Chlorinator.PHP):

<?PHP

class Chlorinator extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
    protected function _construct()
    {
        $this->_init('chlorinators','chlorinator_id');
    }
}

此时,我只是想通过这些模型将数据插入到创建的表中,但我没有任何运气。我正在使用数据补丁尝试插入数据,如下所示:

<?PHP
    /**
     * copyright © Magento,Inc. All rights reserved.
     * See copYING.txt for license details.
     */
    namespace Jared\Submittal\Setup\Patch\Data;

    use Magento\Framework\Setup\Patch\DataPatchInterface;

    /**
     */
    class AddData implements \Magento\Framework\Setup\Patch\DataPatchInterface
    {
        /**
         * @var \Magento\Framework\Setup\ModuleDataSetupInterface
         */
        private $moduleDataSetup;
        private $chlorinator;

        /**
         * @param \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup
         */
        public function __construct(
            \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup,\Jared\Submittal\Model\ResourceModel\Chlorinator $chlorinator
        ){
            /**
             * If before,we pass $setup as argument in install/upgrade function,from Now we start
             * inject it with DI. If you want to use setup,you can inject it,with the same way as here
             */
            $this->moduleDataSetup = $moduleDataSetup;
            $this->chlorinator = $chlorinator;
        }

        /**
         * {@inheritdoc}
         */
        public function apply()
        {
            $chlorinatorData = [];
            $chlorinatorData['chlorinator_id'] = '1';
            $chlorinatorData['chlorinator_name'] = 'chlorinator 1';
            $chlorinatorData['has_pump'] = '1';
            $this->chlorinator->addData($chlorinatorData);
            $this->chlorinator->getResource()->save($this->chlorinator);
        }

        /**
         * {@inheritdoc}
         */
        public static function getDependencies()
        {
            /**
             * This is dependency to another patch. Dependency should be applied first
             * One patch can have few dependencies
             * Patches do not have versions,so if in old approach with Install/Ugrade data scripts you used
             * versions,right Now you need to point from patch with higher version to patch with lower version
             * But please,note,that some of your patches can be independent and can be installed in any sequence
             * So use dependencies only if this important for you
             */
            return [];
        }

        public function revert()
        {
            $this->moduleDataSetup->getConnection()->startSetup();
            //Here should go code that will revert all operations from `apply` method
            //Please note,that some operations,like removing data from column,that is in role of foreign key reference
            //is dangerous,because it can trigger ON DELETE statement
            $this->moduleDataSetup->getConnection()->endSetup();
        }

        /**
         * {@inheritdoc}
         */
        public function getAliases()
        {
            /**
             * This internal Magento method,that means that some patches with time can change their names,* but changing name should not affect installation process,that's why if we will change name of the patch
             * we will add alias here
             */
            return [];
        }

        public static function getVersion()
        {
            return "1.0.0";
        }
    }



我的印象是模型继承了所有 CRUD 操作,但是当我在 AddData.PHP调用 create() 时,我收到调用未定义方法错误。网上的几个模块似乎使用相同的语法和工作,这一事实加剧了我的困惑,例如这个:

https://github.com/cedcommerce/Magento2.3-GraphQl/blob/master/Setup/Patch/Data/AddData.php

我收到的错误已复制到下面,如果您在我的代码中看到导致这些问题的错误,请告诉我:

Fatal error: Uncaught Error: Call to undefined method Jared\Submittal\Model\ResourceModel\Chlorinator::create() in /var/www/html/app/code/Jared/Submittal/Setup/Patch/Data/AddData.PHP:63
Stack trace:
#0 /var/www/html/vendor/magento/framework/Setup/Patch/PatchApplier.PHP(162): Jared\Submittal\Setup\Patch\Data\AddData->apply()
#1 /var/www/html/setup/src/Magento/Setup/Model/Installer.PHP(1081): Magento\Framework\Setup\Patch\PatchApplier->applyDataPatch()
#2 /var/www/html/setup/src/Magento/Setup/Model/Installer.PHP(947): Magento\Setup\Model\Installer->handleDBSchemaData()
#3 /var/www/html/setup/src/Magento/Setup/Console/Command/UpgradeCommand.PHP(147): Magento\Setup\Model\Installer->installDataFixtures()
#4 /var/www/html/vendor/symfony/console/Command/Command.PHP(255): Magento\Setup\Console\Command\UpgradeCommand->execute()
#5 /var/www/html/vendor/symfony/console/Application.PHP(1009): Symfony\Component\Console\Command\Command->run()
#6 /var/www/html/vendor/symfony/console/Application.PHP(273): Symfony\Component\Console\Applic in /var/www/html/app/code/Jared/Submittal/Setup/Patch/Data/AddData.PHP on line 63
{"messages":{"error":[{"code":500,"message":"Fatal error: 'Uncaught Error: Call to undefined method Jared\\Submittal\\Model\\ResourceModel\\Chlorinator::create() in \/var\/www\/html\/app\/code\/Jared\/Submittal\/Setup\/Patch\/Data\/AddData.PHP:63\nStack trace:\n#0 \/var\/www\/html\/vendor\/magento\/framework\/Setup\/Patch\/PatchApplier.PHP(162): Jared\\Submittal\\Setup\\Patch\\Data\\AddData->apply()\n#1 \/var\/www\/html\/setup\/src\/Magento\/Setup\/Model\/Installer.PHP(1081): Magento\\Framework\\Setup\\Patch\\PatchApplier->applyDataPatch()\n#2 \/var\/www\/html\/setup\/src\/Magento\/Setup\/Model\/Installer.PHP(947): Magento\\Setup\\Model\\Installer->handleDBSchemaData()\n#3 \/var\/www\/html\/setup\/src\/Magento\/Setup\/Console\/Command\/UpgradeCommand.PHP(147): Magento\\Setup\\Model\\Installer->installDataFixtures()\n#4 \/var\/www\/html\/vendor\/symfony\/console\/Command\/Command.PHP(255): Magento\\Setup\\Console\\Command\\UpgradeCommand->execute()\n#5 \/var\/www\/html\/vendor\/symfony\/console\/Application.PHP(1009): Symfony\\Component\\Console\\Command\\Command->run()\n#6 \/var\/www\/html\/vendor\/symfony\/console\/Application.PHP(273): Symfony\\Component\\Console\\Applic' in '\/var\/www\/html\/app\/code\/Jared\/Submittal\/Setup\/Patch\/Data\/AddData.PHP' on line 63","trace":"Trace is not available."}]}}

解决方法

在您链接到 apply 函数的示例中,您使用的是模型,而在您的代码中,您使用的是资源模型。因此,在您的 Jared\Submittal\Setup\Patch\Data\AddData 构造函数中,您应该将 \Jared\Submittal\Model\Chlorinator 用于 $chlorinator 而不是您当前使用的资源模型。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。