Renders the primary key.
@return string
765040
public function renderPk(): string
    {
        $output = '';

        if ($this->primaryKey->isComposite()) {
            $output .= "\n" . $this->primaryKey->render($this);
        }

        return $output;
    }
1204400
public function getId()
    {
        if (is_null($this->_id) && isset($this->object)) {
            $this->_id = $this->object->primaryKey;
        }

        return $this->_id;
    }
42338
protected function getId()
    {
        if ($key = $this->manager->config('model_primary_key')) {
            return $this->manager->getInstance()->{$key};
        }

        return $this->manager->getInstance()->getKey();
    }
1086276
public function renderKeys()
	{
		echo CHtml::openTag('div',array(
			'class'=>'keys',
			'style'=>'display:none',
			'title'=>Yii::app()->getRequest()->getUrl(),
		));
		foreach($this->dataProvider->getKeys() as $key)
			echo "<span>".CHtml::encode($key)."</span>";
		echo "</div>\n";
	}
1022054
public function run()
    {
        $keysString = "";
        $primaryKeyString = "";
        $fieldStrings = array();
        
        foreach ($this->m_fields as $field_name => $field)
        {
            $fieldStrings[] = $field->getFieldString();
                
            /* @var $field DatabaseField */
            if ($field->isKey())
            {
                if ($keysString !== "")
                {
                    $keysString .= ", ";
                }
                
                if ($field->isUnique())
                {
                    $keysString .= "UNIQUE ";
                }
                
                $keysString .= "KEY (`" . $field_name . "`) ";
            }
        }
        
        $fieldsString = implode(", ", $fieldStrings);
        
        
        # Add combined keys.
        foreach ($this->m_combinedKeys as $key)
        {
            if ($keysString !== "")
            {
                $keysString .= ", ";
            }

            $keysString .= "KEY (" . implode(",", $key) . ") ";
        }
        
        
        # Add foreign keys        
        if (count($this->m_foreignKeys) != 0)
        {
            foreach ($this->m_foreignKeys as $foreign_key_obj)
            {
                $table_column     = $foreign_key_obj->table_column;
                $reference_table  = $foreign_key_obj->reference_table;
                $reference_column = $foreign_key_obj->reference_column;
                $deleteCascade   = $foreign_key_obj->delete_cascade;
                $updateCascade   = $foreign_key_obj->update_cascade;
                
                if ($keysString !== "")
                {
                    $keysString .= ", ";
                }
                
                $keysString .= 
                    'FOREIGN KEY (`' . $table_column . '`) ' .
                    'REFERENCES `' . $reference_table . '` (`' . $reference_column . '`) ';
                
                if ($deleteCascade)
                {
                    $keysString .= 'ON DELETE CASCADE ';
                }
                
                if ($updateCascade)
                {
                    $keysString .= 'ON UPDATE CASCADE ';
                }
            }
        }
        
        # Add unique combined keys.
        foreach ($this->m_combinedUniqueKeys as $key)
        {
            if ($keysString !== "")
            {
                $keysString .= ", ";
            }
            
            $keysString .= "UNIQUE KEY (" . implode(",", $key) . ") ";
        }
        
        # Add the Primary key (can only be one)
        if ($this->m_primaryKey != null)
        {
            $primaryKeyString = "PRIMARY KEY (" . implode(",", $this->m_primaryKey) . ") ";
        }
        
        $engine_string = "ENGINE=" . $this->m_engine . " ";
        
        
        if ($primaryKeyString != "")
        {
            $primaryKeyString = ", " . $primaryKeyString;
        }
        
        if ($keysString != "")
        {
            $keysString = ", " . $keysString;
        }
        
        $fieldsString .= $primaryKeyString . $keysString;
        
        $characterset_string = "";
        if ($this->m_charSet !== null)
        {
            $characterset_string = "DEFAULT CHARSET=" . $this->m_charSet . " ";
        }
        
        $query = 
            "CREATE TABLE " . 
            '`' . $this->m_name . '` ' . 
            "(" . $fieldsString . ") " . 
            $engine_string . 
            $characterset_string;
        
        $result = $this->m_mysqliConn->query($query);
        
        if ($result !== TRUE)
        {
            $err_array = array(
                "Class"         => get_called_class(),
                "Message"       => "Error creating table",
                "Query"         => $query,
                "Mysqli Error"  => $this->m_mysqliConn->error
            );
                
            throw new \Exception(json_encode($err_array, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES));
        }
    }
628112
protected function getPrimaryKey(): array
    {
        $primary = array_flip($this->getTable()->getPrimaryKey());

        return array_intersect_key($this->toArray(), $primary);
    }
826048
public function getPublicIdAttribute()
    {
        $hashids = new Hashids(self::getSalt(), self::getMinLength(), self::getAlphabet());

        $primaryKey = $this->primaryKey;

        return $hashids->encode($this->$primaryKey);
    }
388787
protected function _setupPrimaryKey()
    {
        if (!$this->_primary) {
            $this->_setupMetadata();
            $this->_primary = array();
            foreach ($this->_metadata as $col) {
                if ($col['PRIMARY']) {
                    $this->_primary[ $col['PRIMARY_POSITION'] ] = $col['COLUMN_NAME'];
                    if ($col['IDENTITY']) {
                        $this->_identity = $col['PRIMARY_POSITION'];
                    }
                }
            }
            // if no primary key was specified and none was found in the metadata
            // then throw an exception.
            if (empty($this->_primary)) {
                // require_once 'Zend/Db/Table/Exception.php';
                throw new Zend_Db_Table_Exception('A table must have a primary key, but none was found');
            }
        } else if (!is_array($this->_primary)) {
            $this->_primary = array(1 => $this->_primary);
        } else if (isset($this->_primary[0])) {
            array_unshift($this->_primary, null);
            unset($this->_primary[0]);
        }

        $cols = $this->_getCols();
        if (! array_intersect((array) $this->_primary, $cols) == (array) $this->_primary) {
            // require_once 'Zend/Db/Table/Exception.php';
            throw new Zend_Db_Table_Exception("Primary key column(s) ("
                . implode(',', (array) $this->_primary)
                . ") are not columns in this table ("
                . implode(',', $cols)
                . ")");
        }

        $primary    = (array) $this->_primary;
        $pkIdentity = $primary[(int) $this->_identity];

        /**
         * Special case for PostgreSQL: a SERIAL key implicitly uses a sequence
         * object whose name is "<table>_<column>_seq".
         */
        if ($this->_sequence === true && $this->_db instanceof Zend_Db_Adapter_Pdo_Pgsql) {
            $this->_sequence = $this->_db->quoteIdentifier("{$this->_name}_{$pkIdentity}_seq");
            if ($this->_schema) {
                $this->_sequence = $this->_db->quoteIdentifier($this->_schema) . '.' . $this->_sequence;
            }
        }
    }
161873
public function key()
    {
        if (empty($this->key) && !empty($this->definition->primary)) {
            $keys = array_values(
                array_filter(
                    preg_split('~[\s,]+~', $this->definition->primary)
                )
            );
            $keys = array_map(
                function ($key) {
                    return Normalise::toVariable($key);
                },
                $keys
            );

            $this->key = count($keys) > 1 ? $keys : array_shift($keys);
        }

        if (empty($this->key) && $this->has('id')) {
            $this->key = 'id';
        }

        return $this->key;
    }
956848
public function render(): string
    {
        return $this->token . $this->escape($this->insert) . $this->token;
    }
594280
public function renderCreateIndexes()
    {
        $indexes = $this->getTableIndexes();
        if (!$indexes) {
            return '';
        }
        $source = '';
        foreach ($indexes as $index) {
            $columns = $this->serializeArrayToAttributes($index->getColumns());
            $name    = $this->serializeIndexNameToAttribute($index->getName());
            if ($index->isPrimary()) {
                $source .= "\n" . '$table->primary(' . $columns . ', ' . $name . ');';
            } elseif ($index->isUnique()) {
                $source .= "\n" . '$table->unique(' . $columns . ', ' . $name . ');';
            } else {
                $source .= "\n" . '$table->index(' . $columns . ', ' . $name . ');';
            }
        }

        return trim($source);
    }
594727
public function toString() {
        $description = $this->getDescription();
        if($description == '') {
            $description = $this->getName();
        }
        $buffer = '	/**
	 * ' . $description . "\n";
        if($this->isPrimary()) {
            $buffer .= '	 * @Primary' . "\n";
        }
        if($this->isAutoIncrementing()) {
            $buffer .= '	 * @Identity' . "\n";
        }
        $buffer .= '	 * @Column(type="' . $this->getType() . '"'
            . (!is_null($this->getLength()) ? ', length=' . $this->getLength() : '')
            . ', nullable=' . ($this->isNullable() ? 'true' : 'false') . ')' . "\n";
        $buffer .= '	 */
	' . $this->getAccess() . ' $' . $this->getName() . ';
';
        return $buffer;
    }
1095144
public function getPrimaryKeyValue()
    {
        $primaryKey = $this->getPrimaryKey();
        if (isset($this->$primaryKey)) {
            return $this->$primaryKey;
        } else {
            return null;
        }
    }
99565
protected function fetchPrimaryKeys(): array
    {
        $primaryKeys = [];
        foreach ($this->columnSchemas() as $column) {
            if (!empty($column['pk'])) {
                $primaryKeys[] = $column['name'];
            }
        }

        return $primaryKeys;
    }
219644
public function getKeyValue()
    {
        if (array_key_exists($this->primary_key, $this->original)) {
            return $this->original[$this->primary_key];
        }

        return null;
    }
296842
public function hashCode()
    {
        $validPk = null !== $this->getUserId() &&
            null !== $this->getRoleId();

        $validPrimaryKeyFKs = 2;
        $primaryKeyFKs = [];

        //relation user_role_fk_29554a to table user
        if ($this->aUser && $hash = spl_object_hash($this->aUser)) {
            $primaryKeyFKs[] = $hash;
        } else {
            $validPrimaryKeyFKs = false;
        }

        //relation user_role_fk_1ff99e to table role
        if ($this->aRole && $hash = spl_object_hash($this->aRole)) {
            $primaryKeyFKs[] = $hash;
        } else {
            $validPrimaryKeyFKs = false;
        }

        if ($validPk) {
            return crc32(json_encode($this->getPrimaryKey(), JSON_UNESCAPED_UNICODE));
        } elseif ($validPrimaryKeyFKs) {
            return crc32(json_encode($primaryKeyFKs, JSON_UNESCAPED_UNICODE));
        }

        return spl_object_hash($this);
    }
127642
public function renderForeignKeys(): string
    {
        $output = '';

        if ($this->foreignKeys) {
            foreach ($this->foreignKeys as $foreignKey) {
                $output .= "\n" . $foreignKey->render($this);
            }
        }

        return $output;
    }