在Magento的安装脚本中添加一个自增列,而不使用SQL

14

之前我问过如何在Magento安装脚本中使用ALTER TABLE而不使用SQL。 在那里,Ivan 给出了一个非常好的答案,现在我仍然参考它。

但是,我还没有发现如何使用Varien_Db_Ddl_Table::addColumn()指定一个auto_increment列。 我认为这与一个称为identity的选项有关,但迄今为止我还没有成功。

这是否可能,还是该功能尚未完整实现?

2个回答

18

在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"。


在这个原始问题提出的一年里,似乎自增已经被引入了。 - clockworkgeek
有没有办法告诉自动增量从10000开始? - Yehuda Schwartz

12

我认为那是尚未实现的功能。

如果你查看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从哪个整数开始。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接