yii\db\BaseActiveRecord::getPrimaryKey PHP Method

getPrimaryKey() public method

Returns the primary key value(s).
public getPrimaryKey ( boolean $asArray = false ) : mixed
$asArray boolean whether to return the primary key value as an array. If `true`, the return value will be an array with column names as keys and column values as values. Note that for composite primary keys, an array will always be returned regardless of this parameter value.
return mixed the primary key value. An array (column name => column value) is returned if the primary key is composite or `$asArray` is `true`. A string is returned otherwise (null will be returned if the key value is null).
    public function getPrimaryKey($asArray = false)
    {
        $keys = $this->primaryKey();
        if (!$asArray && count($keys) === 1) {
            return isset($this->_attributes[$keys[0]]) ? $this->_attributes[$keys[0]] : null;
        } else {
            $values = [];
            foreach ($keys as $name) {
                $values[$name] = isset($this->_attributes[$name]) ? $this->_attributes[$name] : null;
            }
            return $values;
        }
    }

Usage Example

 public static function getPrimaryKey(BaseActiveRecord $model, $separator = ',')
 {
     return implode($separator, array_values($model->getPrimaryKey(true)));
 }