【问题标题】:Add an auto_increment column in Magento setup script without using SQL在 Magento 设置脚本中添加 auto_increment 列而不使用 SQL
【发布时间】:2011-07-17 12:50:42
【问题描述】:

之前我问过如何ALTER TABLE in Magento setup script without using SQL。在那里,Ivan 给出了一个很好的答案,我现在仍然在参考。

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

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

【问题讨论】:

    标签: magento installation auto-increment


    【解决方案1】:

    可以像这样创建一个自动增量列(至少从 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”。

    【讨论】:

    • 自从这个原始问题以来的一年里,似乎已经引入了自动增量。
    • 有什么方法可以告诉自动增量从 10000 开始?
    【解决方案2】:

    我认为这还没有实现。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-07
      • 2019-05-28
      • 2018-11-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-02
      相关资源
      最近更新 更多