【问题标题】:Magento 2 auto create custom table from custom moduleMagento 2 从自定义模块自动创建自定义表
【发布时间】:2015-12-16 09:49:21
【问题描述】:

我刚从 Magento 1.7 升级。我成功创建了模块、控制器、助手等。

今天我尝试使用自定义表格创建模型。我希望自动创建表。我做了以下步骤:

我创建了一个具有以下目录结构的模块:

<Ashutosh>
  <Pandey>
      <etc>
          module.xml
      <Model>
      <Setup>
          InstallSchema.php

这是每个文件的内容:

InstallSchema.php

<?php

namespace Ashutosh\Pandey\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\DB\Ddl\Table;

class InstallSchema implements InstallSchemaInterface
{
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;
        $installer->startSetup();

        // Get table
        $tableName = $installer->getTable('ashutosh_friends');

        // Check if the table already exists
        if ($installer->getConnection()->isTableExists($tableName) != true) {
            // Create tutorial_simplenews table
            $table = $installer->getConnection()
                ->newTable($tableName)
                ->addColumn(
                    'id',
                    Table::TYPE_INTEGER,
                    null,
                    [
                        'identity' => true,
                        'unsigned' => true,
                        'nullable' => false,
                        'primary' => true
                    ],
                    'ID'
                )
                ->addColumn(
                    'name',
                    Table::TYPE_TEXT,
                    null,
                    ['nullable' => false, 'default' => ''],
                    'Title'
                )
                ->addColumn(
                    'friend_type',
                    Table::TYPE_TEXT,
                    null,
                    ['nullable' => false, 'default' => ''],
                    'Summary'
                )
                ->addColumn(
                    'created_at',
                    Table::TYPE_DATETIME,
                    null,
                    ['nullable' => false],
                    'Created At'
                )
                ->addColumn(
                    'status',
                    Table::TYPE_SMALLINT,
                    null,
                    ['nullable' => false, 'default' => '0'],
                    'Status'
                )
                ->setComment('Ashutosh Friends Table')
                ->setOption('type', 'InnoDB')
                ->setOption('charset', 'utf8');
            $installer->getConnection()->createTable($table);
        }

        $installer->endSetup();
    }
}

module.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Ashutosh_Pandey" setup_version="1.0.0" active="true"/>
</config>

然后在我的 magento2 目录中的命令提示符下,我执行了命令:

php bin/magento setup:upgrade

该命令成功运行,但未创建表。 然后我也执行了命令:

php bin/magento setup:db-schema:upgrade

但仍然没有任何反应。

注意:如果我在数据库中手动创建表,我可以成功读取它。

【问题讨论】:

    标签: model magento2 magento-2.0


    【解决方案1】:

    Magento 将 module.xml 中指定的模块版本与数据库表 setup_module 中的条目进行匹配。您可以尝试删除自定义模块的条目,该条目应触发 magento 在使用 php bin/magento setup:upgrade 时再次运行安装脚本:升级

    select * from core_config_data;
    
    delete from setup_module where module="<YOURMODULE>";
    

    【讨论】:

    • 简直太棒了伙计.. +1 来自我 :-)
    【解决方案2】:

    改变

    if ($installer-&gt;getConnection()-&gt;isTableExists($tableName) != true) {

    到任何一个

    if (!$installer-&gt;getConnection()-&gt;isTableExists($tableName)) {

    if ($installer-&gt;getConnection()-&gt;isTableExists($tableName) !== true) {

    差别很小。见http://php.net/manual/en/language.operators.comparison.php

    【讨论】:

    • 嗨,Maddy,我确实使用了上述运算符,但看起来 Magento2 没有选择我的 InstallScript.php。我遵循了多个这样的例子:ashsmith.io/magento2/module-from-scratch-part-3-database-tables
    • 我故意放了一个错误的语句,看起来它正在读取 InstallSchema 文件,但没有创建表。另外,我删除了 if() 语句并尝试直接创建表。
    • 您可以使用 xdebug 查看为什么它没有创建表。我就是这样测试的。它有效。
    【解决方案3】:

    您需要使用 UpgradeSchema.php,因为该模块已经安装。

    基本上与InstallSchema.php几乎相同,但函数名称重命名为升级,并实现类UpgradeSchemaInterface而不是InstallSchemaInterface

    然后运行magento setup:upgrade就可以了

    这是一个 magento 问题,已回答 here

    【讨论】:

      猜你喜欢
      • 2014-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-25
      • 2012-08-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多