yii\db\QueryBuilder::addPrimaryKey PHP Method

addPrimaryKey() public method

Builds a SQL statement for adding a primary key constraint to an existing table.
public addPrimaryKey ( string $name, string $table, string | array $columns ) : string
$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.
return string the SQL statement for adding a primary key constraint to an existing table.
    public function addPrimaryKey($name, $table, $columns)
    {
        if (is_string($columns)) {
            $columns = preg_split('/\\s*,\\s*/', $columns, -1, PREG_SPLIT_NO_EMPTY);
        }
        foreach ($columns as $i => $col) {
            $columns[$i] = $this->db->quoteColumnName($col);
        }
        return 'ALTER TABLE ' . $this->db->quoteTableName($table) . ' ADD CONSTRAINT ' . $this->db->quoteColumnName($name) . '  PRIMARY KEY (' . implode(', ', $columns) . ' )';
    }