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

clo*_*eek 14 installation auto-increment magento

以前我问过如何在不使用SQL的情况下在Magento安装脚本中使用ALTER TABLE.在那里,伊万给出了一个很好的答案,我现在仍然提到这个答案.

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

这甚至可能还是功能不完整?

fee*_*ela 17

可以像这样创建一个自动增量列(至少从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' );
Run Code Online (Sandbox Code Playgroud)

也可以使用关键字"identity"代替"auto_increment".


Ala*_*orm 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
);
Run Code Online (Sandbox Code Playgroud)

但是,如果您查看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);
}
Run Code Online (Sandbox Code Playgroud)

你可以看到_getColumnsDefinition,_getIndexesDefinition_getForeignKeysDefinition用来创建一个CREATE SQL片段.这些方法都没有引用identity或者auto_increment它们似乎也没有生成任何可以创建自动增量的sql.

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

/**
 * Autoincrement for bind value
 *
 * @var int
 */
protected $_bindIncrement       = 0;
Run Code Online (Sandbox Code Playgroud)

用于控制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;
}
Run Code Online (Sandbox Code Playgroud)

但这用于处理上设置的选项.它auto_increment控制表AUTO_INCREMENT选项,可用于控制从哪个整数AUTO_INCREMENT开始.