在 Magento 设置脚本中添加 auto_increment 列,而不使用 SQL

发布于 2024-10-22 11:38:57 字数 377 浏览 2 评论 0原文

之前我问过如何在不使用 SQL 的情况下在 Magento 设置脚本中更改表。在那里,Ivan 给出了一个很好的答案,我至今仍在参考。

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

这是否可能,或者该功能是否不完整?

Previously I asked how to ALTER TABLE in Magento setup script without using SQL. There, Ivan gave an excellent answer which I still refer to even now.

However I have yet to discover how to use Varien_Db_Ddl_Table::addColumn() to specify an auto_increment column. I think it has something to do with an option called identity but so far have had no luck.

Is this even possible or is that functionality incomplete?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你的心境我的脸 2024-10-29 11:38:57

人们可以创建这样的自动增量列(至少从 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' );

除了“auto_increment”之外,还可以使用关键字“identity”。

One can create an autoincrement column like that (at least since Magento 1.6, maybe even earlier):

/** @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' );

Instead of "auto_increment", one may also use the keyword "identity".

养猫人 2024-10-29 11:38:57

我认为这是尚未实施的事情。

如果您查看 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_INCRMENT选项,该选项可用于控制AUTO_INCRMENT从哪个整数开始。

I think that's something that hasn't been implemented yet.

If you look at the source to addColumn, you can see it looks for a identity/auto_increment option and sets an IDENTITY attribute on the internal column representation.

#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
);

However, if you look at the createTable method on the connection object

#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);
}

you can see _getColumnsDefinition, _getIndexesDefinition, and _getForeignKeysDefinition are used to create a CREATE SQL fragment. None of these methods make any reference to identity or auto_increment, nor do they appear to generate any sql that would create an auto increment.

The only possible candidates in this class are

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

which is used to control the increment number for a PDO bound parameter (nothing to do with auto_increment).

There's also a mention of auto_increment here

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;
}

but this is used to process options set on the table. This auto_increment controls the table AUTO_INCREMENT options, which can be used to control which integer an AUTO_INCREMENT starts at.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文