为何在Magento安装脚本中不使用SQL更改表?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (37)

我想知道如何将列或索引添加/修改/移除到表中,而不依赖SQL?有可能吗?

此外,只有在SQL中才能执行其他操作?

提问于
用户回答回答于

  • 使用安装连接($this->getConnection())方法:
- `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)

用户回答回答于

  1. Magento对象在数据库/数据存储层之上提供抽象。
  2. 您应该使用抽象来更新Magento,这可以确保Magento团队更改对象与数据存储的交互方式,您的更新仍然可以工作(假设核心团队维护对象方法所隐含的原始“契约”)。

所以,问题是ALTER TABLE陈述直接变化数据存储。不应该更改数据存储区(在添加列或索引的情况下,这意味着只使用Eav模型,使用Setup Resources来管理更改,并接受Magento的索引)。

扫码关注云+社区