在Magento安装脚本中添加auto_increment列而不使用SQL

时间:2011-03-17 16:00:14

标签: magento installation auto-increment

以前我问过如何ALTER TABLE in Magento setup script without using SQL。在那里,Ivan提供了一个很好的答案,我现在仍然提到它。

但是,我还没有发现如何使用Varien_Db_Ddl_Table::addColumn()来指定auto_increment列。我认为它与名为identity的选项有关,但到目前为止没有运气。

这是否可能或功能不完整?

2 个答案:

答案 0 :(得分:16)

可以像这样创建一个自动增量列(至少从Magento 1.6开始,甚至可能更早):

/** @var $table Varien_Db_Ddl_Table */
$table->addColumn( 'id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
    'auto_increment' => true,
    'unsigned' => true,
    'nullable' => false,
    'primary' => true,
), 'ID' );

也可以使用关键字“identity”来代替“auto_increment”。

答案 1 :(得分:11)

我认为这还没有实现。

如果您查看addColumn的来源,可以看到它查找identity/auto_increment选项,并在内部列表示中设置IDENTITY属性。

#File: lib/Varien/Db/Ddl/Table.php
if (!empty($options['identity']) || !empty($options['auto_increment'])) {
    $identity = true;
}

$upperName = strtoupper($name);
$this->_columns[$upperName] = array(
    'COLUMN_NAME'       => $name,
    'COLUMN_TYPE'       => $type,
    'COLUMN_POSITION'   => $position,
    'DATA_TYPE'         => $type,
    'DEFAULT'           => $default,
    'NULLABLE'          => $nullable,
    'LENGTH'            => $length,
    'SCALE'             => $scale,
    'PRECISION'         => $precision,
    'UNSIGNED'          => $unsigned,
    'PRIMARY'           => $primary,
    'PRIMARY_POSITION'  => $primaryPosition,
    'IDENTITY'          => $identity
);

但是,如果查看连接对象上的createTable方法

#File: lib/Varien/Db/Adapter/Pdo/Mysql.php
public function createTable(Varien_Db_Ddl_Table $table)
{
    $sqlFragment    = array_merge(
        $this->_getColumnsDefinition($table),
        $this->_getIndexesDefinition($table),
        $this->_getForeignKeysDefinition($table)
    );
    $tableOptions   = $this->_getOptionsDefination($table);

    $sql = sprintf("CREATE TABLE %s (\n%s\n) %s",
        $this->quoteIdentifier($table->getName()),
        implode(",\n", $sqlFragment),
        implode(" ", $tableOptions));

    return $this->query($sql);
}

您可以看到_getColumnsDefinition_getIndexesDefinition_getForeignKeysDefinition用于创建CREATE SQL片段。这些方法都没有对identityauto_increment进行任何引用,也不会生成任何会创建自动增量的sql。

本课程中唯一可能的候选人是

/**
 * Autoincrement for bind value
 *
 * @var int
 */
protected $_bindIncrement       = 0;

用于控制PDO绑定参数的增量编号(与auto_increment无关)。

此处还提到auto_increment

protected function _getOptionsDefination(Varien_Db_Ddl_Table $table)
{
    $definition = array();
    $tableProps = array(
        'type'              => 'ENGINE=%s',
        'checksum'          => 'CHECKSUM=%d',
        'auto_increment'    => 'AUTO_INCREMENT=%d',
        'avg_row_length'    => 'AVG_ROW_LENGTH=%d',
        'comment'           => 'COMMENT=\'%s\'',
        'max_rows'          => 'MAX_ROWS=%d',
        'min_rows'          => 'MIN_ROWS=%d',
        'delay_key_write'   => 'DELAY_KEY_WRITE=%d',
        'row_format'        => 'row_format=%s',
        'charset'           => 'charset=%s',
        'collate'           => 'COLLATE=%s'
    );
    foreach ($tableProps as $key => $mask) {
        $v = $table->getOption($key);
        if (!is_null($v)) {
            $definition[] = sprintf($mask, $v);
        }
    }

    return $definition;
}

但这用于处理表格上设置的选项。此auto_increment控制表AUTO_INCREMENT选项,可用于控制AUTO_INCREMENT开始的整数。