没有不值得去解决的问题,也没有不值得去学习的技术!

Implement a String Primary Key With Database Migrations in Yii 2.0

A colleague plans to create a primary key column of type varchar(32).

1. In Yii 2.0, create a primary key column using a database migration. By default, it uses $this->primaryKey(). The created primary key column defaults to the int(11) type. See Figure 1.

In Yii 2.0, create a primary key column using a database migration. By default, it uses $this->primaryKey(). The created primary key column defaults to the int(11) type.
Figure 1

2. A colleague plans to create a primary key column of type varchar(32). See Figure 2.

A colleague plans to create a primary key column of type varchar(32).
Figure 2

3. The primaryKey() method only accepts a single column size parameter. Therefore, a string-type primary key cannot be created using this method.

PHP

    /**
     * Creates a primary key column.
     * @param int $length column size or precision definition.
     * This parameter will be ignored if not supported by the DBMS.
     * @return ColumnSchemaBuilder the column instance which can be further customized.
     * @since 2.0.6
     */
    public function primaryKey($length = null)
    {
        return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_PK, $length);
    }

4. Finally, I decided to first create a string-type field, and after the table is created, set the field as the primary key (using the addPrimaryKey() method). This creates a primary key on an existing field.

PHP

    /**
     * Builds and executes a SQL statement for creating a primary key.
     * The method will properly quote the table and column names.
     * @param string $name the name of the primary key constraint.
     * @param string $table the table that the primary key constraint will be added to.
     * @param string|array $columns comma separated string or array of columns that the primary key will consist of.
     */
    public function addPrimaryKey($name, $table, $columns)
    {
        $time = $this->beginCommand("add primary key $name on $table (" . (is_array($columns) ? implode(',', $columns) : $columns) . ')');
        $this->db->createCommand()->addPrimaryKey($name, $table, $columns)->execute();
        $this->endCommand($time);
    }

5. The final code implementation is as follows. See Figure 3.

The final code implementation is as follows
Figure 3
PHP

        $tableOptions = null;
        if ($this->db->driverName === 'mysql') {
            $tableOptions = 'CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE=InnoDB COMMENT="渠道"';
        };
        $this->createTable('{{%ccp_platform_account}}', [
            'account_uuid' => $this->string(32)->notNull(),
        ], $tableOptions);

        $this->addPrimaryKey('account_uuid','{{%ccp_platform_account}}','account_uuid');

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系