yii\db\Migration::addPrimaryKey PHP Method

addPrimaryKey() public method

The method will properly quote the table and column names.
public addPrimaryKey ( string $name, string $table, string | array $columns )
$name string the name of the primary key constraint.
$table string the table that the primary key constraint will be added to.
$columns string | array comma separated string or array of columns that the primary key will consist of.
    public function addPrimaryKey($name, $table, $columns)
    {
        echo "    > add primary key {$name} on {$table} (" . (is_array($columns) ? implode(',', $columns) : $columns) . ') ...';
        $time = microtime(true);
        $this->db->createCommand()->addPrimaryKey($name, $table, $columns)->execute();
        echo ' done (time: ' . sprintf('%.3f', microtime(true) - $time) . "s)\n";
    }

Usage Example

コード例 #1
0
 /**
  * Create table in database
  */
 public function runCreateTable()
 {
     $this->dropTable();
     if ($this->hideMigrationOutput) {
         ob_start();
     }
     /** @var Connection $_conn */
     $_conn = Yii::$app->{$this->db};
     if (!$_conn->schema->getTableSchema($this->tableName)) {
         $this->migrationClass->createTable($this->tableNameRaw, $this->columns);
         if (is_array($this->primaryKeys) && sizeof($this->primaryKeys)) {
             try {
                 $this->migrationClass->addPrimaryKey("{$this->tableNameRaw}_pk", $this->tableNameRaw, $this->primaryKeys);
             } catch (\yii\db\Exception $exp) {
             }
         }
     }
     if ($this->hideMigrationOutput) {
         ob_clean();
         ob_flush();
     }
 }
All Usage Examples Of yii\db\Migration::addPrimaryKey