不使用SQL的Magento安装脚本中的ALTER TABLE [英] ALTER TABLE in Magento setup script without using SQL
问题描述
乔丹节说
"更新不应采用以下形式 SQL命令".我还没碰到 任何不能的DDL或DML陈述 通过Magento的配置执行 结构.
"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.
我想知道如何最好地以这种方式在表中添加/修改/删除表中的列或索引,但又不依赖SQL?甚至有可能吗?
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?
此外,还有哪些其他操作只能在SQL中完成?
Furthermore, what other actions can only be done in SQL?
推荐答案
您可以在安装脚本中使用以下方法:
You can use such methods within your setup script:
-
使用
Varien_Db_Ddl_Table
类创建新表,您可以在其中与$this->getConnection()->createTable($tableObject)
组合配置所有字段,键,关系 示例:
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);
使用设置连接($this->getConnection()
)方法:
Use setup connection ($this->getConnection()
) methods:
-
addColumn()
方法将新列添加到退出表.它具有以下参数:-
$tableName
-应该修改的表名 -
$columnName
-应添加的列名 -
$definition
-列的定义(INT(10)
,DECIMAL(12,4)
等)
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)
-
$fkName
-外键名称在每个数据库中应该是唯一的,如果不指定FK_
前缀,则会自动添加 -
$tableName
-用于添加外键的表名 -
$columnName
-应该引用到另一个表的列名,如果您具有复杂的外键,请使用逗号指定多个列 -
$refTableName
-将要处理的外部表名称 -
$refColumnName
-外部表中的列名 -
$onDelete
-在外部表中删除行的操作.可以为空字符串(不执行任何操作),cascade
,set null
.该字段是可选的,如果未指定,将使用cascade
值. -
$onUpdate
在外表中更新行键的操作.可以为空字符串(不执行任何操作),cascade
,set null
.该字段是可选的,如果未指定,将使用cascade
值. -
$purge
-用于在添加外键后清除行的标志(例如,删除未引用的记录)
$fkName
- the foreign key name, should be unique per database, if you don't specifyFK_
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)
-
$tableName
-应该在其中添加索引的表名 -
$indexName
-索引名称 -
$fields
-索引中使用的列名 -
$indexType
-索引的类型.可能的值为:index
,unique
,primary
,fulltext
.此参数是可选的,因此默认值为index
$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 isindex
-
$tableName
-应该修改的表名 -
$columnName
-应该删除的列的名称
$tableName
- the table name that should be modified$columnName
- the name of the column, that should removed
-
$tableName
-删除外键的表名 -
$fkName
-外键名称
$tableName
- the table name for removing a foreign key$fkName
- the foreign key name
-
$tableName
-应该在其中删除索引的表名 -
$keyName
-索引名称
$tableName
- the table name where the index should be removed$keyName
- the index name
-
$tableName
-应该修改的表名 -
$columnName
-列名,应重命名 -
$definition
-列的新定义(INT(10)
,DECIMAL(12,4)
等)
$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)
-
$tableName
-应该修改的表名 -
$oldColumnName
-列的旧名称,应重命名和修改 -
$newColumnName
-列的新名称 -
$definition
-列的新定义(INT(10)
,DECIMAL(12,4)
等)
$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)
-
$tableName
-表名 -
$engine
-新引擎名称(MEMORY
,MyISAM
,InnoDB
等)
$tableName
- the table name$engine
- new engine name (MEMORY
,MyISAM
,InnoDB
, etc)
还可以使用
tableColumnExists
方法检查列的存在.Also you can use
tableColumnExists
method to check existence of the column.摆脱直接SQL查询编写的方法列表并不完整.您可以在
Varien_Db_Adapter_Pdo_Mysql
和Zend_Db_Adapter_Abstract
类中找到更多信息.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
andZend_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 :)
这篇关于不使用SQL的Magento安装脚本中的ALTER TABLE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
-