Magento 设置脚本中的 ALTER TABLE 不使用 SQL

发布于 2024-10-04 19:49:18 字数 363 浏览 4 评论 0原文

乔纳森·戴

“更新不应采用以下形式 SQL命令”。我没遇到过 任何 DDL 或 DML 语句不能 通过 Magento 的配置执行 结构。

(在问题如何将配置更改从开发环境迁移到生产环境?

我想知道如何最好地添加以这种方式在表中/修改/删除列或索引,但不依赖 SQL?有可能吗?

此外,还有哪些操作只能在 SQL 中完成?

Jonathon Day says

"updates SHOULD NOT be in the form of
SQL commands". I haven't come across
any DDL or DML statments that cannot
be executed via Magento's config
structures.

(In the question How can I migrate configuration changes from development to production environment?)

I would like to know how best to add/modify/remove a column or index to/from a table in this manner, but without relying on SQL? Is it even possible?

Furthermore, what other actions can only be done in SQL?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

顾挽 2024-10-11 19:49:18

您可以在设置脚本中使用此类方法:

  • 使用 Varien_Db_Ddl_Table 类创建新表,您可以在其中与 $this-> 组合配置所有字段、键、关系getConnection()->createTable($tableObject)
    示例:

    /* @var $this Mage_Core_Model_Resource_Setup */
    $table = new Varien_Db_Ddl_Table();
    $table->setName($this->getTable('模块/表'));
    $table->addColumn('id', Varien_Db_Ddl_Table::TYPE_INT, 10, 
                      array('无符号' => true, 'primary' => true));
    
    $table->addColumn('名称', Varien_Db_Ddl_Table::TYPE_VARCHAR, 255);
    $table->addIndex('名称', '名称');
    $table->setOption('type', 'InnoDB');
    $table->setOption('charset', 'utf8');
    
    $this->getConnection()->createTable($table);
    
  • 使用设置连接 ($this->getConnection()) 方法:

    • addColumn() 方法将新列添加到现有表中。它有这样的参数:
      • $tableName - 应修改的表名称
      • $columnName - 应添加的列的名称
      • $definition - 列的定义(INT(10)DECIMAL(12,4) 等)
    • addConstraint() 方法创建一个新的约束外键。有这样的参数
      • $fkName - 外键名称,每个数据库应该是唯一的,如果不指定FK_前缀,它将自动添加
      • $tableName - 添加外键的表名
      • $columnName - 应该引用另一个表的列名,如果您有复杂的外键,请使用逗号指定多个列
      • $refTableName - 将被处理的外部表名称
      • $refColumnName - 外部表中的列名称
      • $onDelete - 在外部表中删除行的操作。可以是空字符串(不执行任何操作)、cascadeset null。该字段是可选的,如果未指定,将使用cascade值。
      • $onUpdate 对外部表中行键更新的操作。可以是空字符串(不执行任何操作)、cascadeset null。该字段是可选的,如果未指定,将使用cascade值。
      • $purge - 用于在添加外键后启用行清理的标志(例如删除未引用的记录)
    • addKey() 方法用于向表添加索引。它有这样的参数:
      • $tableName - 应添加索引的表名称
      • $indexName - 索引名称
      • $fields - 索引中使用的列名称
      • $indexType - 索引的类型。可能的值为:indexuniqueprimaryfulltext。该参数是可选的,因此默认值为index
    • dropColumn() 方法用于从现有表中删除列。它有这样的参数:
      • $tableName - 应修改的表名称
      • $columnName - 应删除的列的名称
    • dropForeignKey() 方法用于删除外键。它有这样的参数:
      • $tableName - 用于删除外键的表名称
      • $fkName - 外键名称
    • dropKey() 方法用于删除表索引。它有这样的参数:
      • $tableName - 应删除索引的表名称
      • $keyName - 索引名称
    • modifyColumn 方法用于修改表中现有的列。它有这样的参数:
      • $tableName - 应修改的表名称
      • $columnName - 应重命名的列的名称
      • $definition - 列的新定义(INT(10)DECIMAL(12,4) 等)
    • changeColumn 方法用于修改和重命名表中现有的列。它有这样的参数:
      • $tableName - 应修改的表名称
      • $oldColumnName - 应重命名和修改的旧列名称
      • $newColumnName- 列的新名称
      • $definition - 列的新定义(INT(10)DECIMAL(12,4) 等)
    • changeTableEngine方法用于更改表引擎,例如从MyISAM更改为InnoDB。它有这样的参数:
      • $tableName - 表名称
      • $engine - 新引擎名称(MEMORYMyISAMInnoDB 等)

您也可以使用 tableColumnExists 方法来检查列是否存在。

这不是您可以使用的摆脱直接 SQL 查询编写的方法的完整列表。您可以在 Varien_Db_Adapter_Pdo_MysqlZend_Db_Adapter_Abstract 类中找到更多信息。

不要犹豫,查看您将要使用的类定义,您可以为自己找到很多有趣的东西:)

You can use such methods within your setup script:

  • Use Varien_Db_Ddl_Table class to create new tables, where you can configure all the fields, keys, relations in combination with $this->getConnection()->createTable($tableObject)
    Example:

    /* @var $this Mage_Core_Model_Resource_Setup */
    $table = new Varien_Db_Ddl_Table();
    $table->setName($this->getTable('module/table'));
    $table->addColumn('id', Varien_Db_Ddl_Table::TYPE_INT, 10, 
                      array('unsigned' => true, 'primary' => true));
    
    $table->addColumn('name', Varien_Db_Ddl_Table::TYPE_VARCHAR, 255);
    $table->addIndex('name', 'name');
    $table->setOption('type', 'InnoDB');
    $table->setOption('charset', 'utf8');
    
    $this->getConnection()->createTable($table);
    
  • Use setup connection ($this->getConnection()) methods:

    • addColumn() method adds new column to exiting table. It has such parameters:
      • $tableName - the table name that should be modified
      • $columnName- the name of the column, that should be added
      • $definition - definition of the column (INT(10), DECIMAL(12,4), etc)
    • addConstraint() method creates a new constraint foreign key. It has such parameters
      • $fkName - the foreign key name, should be unique per database, if you don't specify FK_ prefix, it will be added automatically
      • $tableName - the table name for adding a foreign key
      • $columnName - the column name that should be referred to another table, if you have complex foreign key, use comma to specify more than one column
      • $refTableName - the foreign table name, which will be handled
      • $refColumnName - the column name(s) in the foreign table
      • $onDelete - action on row removing in the foreign table. Can be empty string (do nothing), cascade, set null. This field is optional, and if it is not specified, cascade value will be used.
      • $onUpdate action on row key updating in the foreign table. Can be empty string (do nothing), cascade, set null. This field is optional, and if it is not specified, cascade value will be used.
      • $purge - a flag for enabling cleaning of the rows after foreign key adding (e.g. remove the records that are not referenced)
    • addKey() method is used for adding of indexes to a table. It has such parameters:
      • $tableName - the table name where the index should be added
      • $indexName - the index name
      • $fields - column name(s) used in the index
      • $indexType - type of the index. Possible values are: index, unique, primary, fulltext. This parameter is optional, so the default value is index
    • dropColumn() method is used for removing of columns from the existing table. It has such parameters:
      • $tableName - the table name that should be modified
      • $columnName- the name of the column, that should removed
    • dropForeignKey() method is used for removing of foreign keys. It has such parameters:
      • $tableName - the table name for removing a foreign key
      • $fkName - the foreign key name
    • dropKey() method is used for removing of the table indexes. It has such parameters:
      • $tableName - the table name where the index should be removed
      • $keyName - the index name
    • modifyColumn method is used to modify existing column in the table. It has such parameters:
      • $tableName - the table name that should be modified
      • $columnName- the name of the column, that should be renamed
      • $definition - a new definition of the column (INT(10), DECIMAL(12,4), etc)
    • changeColumn method is used to modify and rename existing column in the table. It has such parameters:
      • $tableName - the table name that should be modified
      • $oldColumnName- the old name of the column, that should be renamed and modified
      • $newColumnName- a new name of the column
      • $definition - a new definition of the column (INT(10), DECIMAL(12,4), etc)
    • changeTableEngine method is used to change table engine, from MyISAM to InnoDB for instance. It has such parameters:
      • $tableName - the table name
      • $engine - new engine name (MEMORY, MyISAM, InnoDB, etc)

Also you can use tableColumnExists method to check existence of the column.

It is not the full list of methods that are available for you, to get rid of direct SQL queries writing. You can find more at Varien_Db_Adapter_Pdo_Mysql and Zend_Db_Adapter_Abstract classes.

Do not hesitate to look into the class definition which you are going to use, you can find a lot of interesting things for yourself :)

我家小可爱 2024-10-11 19:49:18

任何 Magento 更新不应包含 SQL 的想法是基于这样的想法:

  1. Magento 对象在数据库/数据存储层之上提供抽象

  2. 您应该使用抽象来更新 Magento,这确保即使 Magento 团队更改了对象与数据存储的交互方式,您的更新仍然有效(假设核心团队维护对象方法隐含的原始“合同”)

所以,问题是一个 ALTER TABLE 语句,直接更改数据存储。如果您完全同意上述两个想法,那么您永远不应该更改数据存储。 (在添加列或索引的情况下,意味着专门使用 EAV 模型、使用设置资源来管理更改并接受 Magento 的索引)。

一个好的一般经验法则是,如果您要更改或添加某些 Magento 核心功能(产品、评论等),请避免直接更改数据库结构,除非您愿意在升级过程中仔细管理它。

如果您正在构建新的对象和功能,请使用您想要创建的任何 SQL,并通过设置资源更改表。如果您查看安装程序/升级文件,您会发现 Magento 核心团队自己执行此操作。

The idea that any Magento updates SHOULD NOT include SQL is based on the idea that

  1. Magento Objects provide abstractions on top of you database/datastore layer

  2. You should use the abstractions to update Magento, which ensures if the Magento team changes how the objects interact with the datastore, your updates will still work (assuming the core team maintains the original "contracts" implied by the Object methods)

So, the problem is an ALTER TABLE statement directly changes the datastore. If you subscribe exclusively to the above two ideas, you should never be changing the data store. (which, in the case of adding a column or an index means using EAV models exclusively, using the Setup Resources to manage changes, and accepting Magento's indexing).

A good general rule of thumb is, if you're changing or adding onto some core Magento functionality (Products, Reviews, etc.), stay away from directly changing the database structure unless you're willing to carefully manage it during upgrades.

If you're building new objects and functionality use whatever SQL you want to create and change your tables via Setup Resources. If you look at the installer/upgrade files you can see that the core Magento team does this themselves.

最终幸福 2024-10-11 19:49:18

要更改表并添加带有外键的列,我已使用 Magento CE v1.6.1.0 成功使用了此方法:

// Alter table to add column
$installer->getConnection()

        ->addColumn(
            $installer->getTable('modulekey/model'), 
            'column_name',  
            array(
                'type'      => Varien_Db_Ddl_Table::TYPE_INTEGER,
                'length'    => null,
                'unsigned'  => true,
                'nullable'  => true,
                'comment'   => 'Foreign key'
            )
        );

// Add foreign key constraint
$installer->getConnection()

        ->addForeignKey(
            $installer->getFkName( 
                'modulekey/model',  'column_name',
                'modulekey/foreign_model',  'foreign_column_name'
            ),
            $installer->getTable('modulekey/model'), 
            'column_name',
            $installer->getTable('modulekey/foreign_model'),
            'foreign_column_name',
            Varien_Db_Ddl_Table::ACTION_SET_NULL, 
            Varien_Db_Ddl_Table::ACTION_SET_NULL
        );

这些是来自 Varien_Db_Adapter_Pdo_Mysql 的方法。

To alter table and add column with a foreign key, I have used this successfully using Magento CE v1.6.1.0 :

// Alter table to add column
$installer->getConnection()

        ->addColumn(
            $installer->getTable('modulekey/model'), 
            'column_name',  
            array(
                'type'      => Varien_Db_Ddl_Table::TYPE_INTEGER,
                'length'    => null,
                'unsigned'  => true,
                'nullable'  => true,
                'comment'   => 'Foreign key'
            )
        );

// Add foreign key constraint
$installer->getConnection()

        ->addForeignKey(
            $installer->getFkName( 
                'modulekey/model',  'column_name',
                'modulekey/foreign_model',  'foreign_column_name'
            ),
            $installer->getTable('modulekey/model'), 
            'column_name',
            $installer->getTable('modulekey/foreign_model'),
            'foreign_column_name',
            Varien_Db_Ddl_Table::ACTION_SET_NULL, 
            Varien_Db_Ddl_Table::ACTION_SET_NULL
        );

Those are methods from Varien_Db_Adapter_Pdo_Mysql.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文