Final Class Yiisoft\Rbac\Db\ItemTreeTraversal\PostgresCteItemTreeTraversal
An RBAC item tree traversal strategy based on CTE (common table expression) for PostgreSQL.
Protected Properties
| Property | Type | Description | Defined By |
|---|---|---|---|
| $childrenTableName | string | Yiisoft\Rbac\Db\ItemTreeTraversal\CteItemTreeTraversal | |
| $database | \Yiisoft\Db\Connection\ConnectionInterface | Yiisoft\Rbac\Db\ItemTreeTraversal\CteItemTreeTraversal | |
| $namesSeparator | string | Yiisoft\Rbac\Db\ItemTreeTraversal\CteItemTreeTraversal | |
| $tableName | string | Yiisoft\Rbac\Db\ItemTreeTraversal\CteItemTreeTraversal |
Public Methods
Protected Methods
Method Details
| public mixed __construct ( \Yiisoft\Db\Connection\ConnectionInterface $database, string $tableName, string $childrenTableName, string $namesSeparator ) | ||
| $database | \Yiisoft\Db\Connection\ConnectionInterface |
Yii Database connection instance. |
| $tableName | string |
A name of the table for storing RBAC items. |
| $childrenTableName | string |
A name of the table for storing relations between RBAC items. |
| $namesSeparator | string |
Separator used for joining item names. |
public function __construct(
protected ConnectionInterface $database,
protected string $tableName,
protected string $childrenTableName,
protected string $namesSeparator,
) {
}
| public array getChildPermissionRows ( string|array $names ) | ||
| $names | string|array | |
public function getChildPermissionRows(string|array $names): array
{
$baseOuterQuery = $this->getChildrenBaseOuterQuery($names)->andWhere(['item.type' => Item::TYPE_PERMISSION]);
/** @psalm-var RawItem[] */
return $this->getRowsCommand($names, baseOuterQuery: $baseOuterQuery, areParents: false)->queryAll();
}
| public array getChildRoleRows ( string|array $names ) | ||
| $names | string|array | |
public function getChildRoleRows(string|array $names): array
{
$baseOuterQuery = $this->getChildrenBaseOuterQuery($names)->andWhere(['item.type' => Item::TYPE_ROLE]);
/** @psalm-var RawItem[] */
return $this->getRowsCommand($names, baseOuterQuery: $baseOuterQuery, areParents: false)->queryAll();
}
| public array getChildrenRows ( string|array $names ) | ||
| $names | string|array | |
public function getChildrenRows(string|array $names): array
{
$baseOuterQuery = $this->getChildrenBaseOuterQuery($names);
/** @psalm-var RawItem[] */
return $this->getRowsCommand($names, baseOuterQuery: $baseOuterQuery, areParents: false)->queryAll();
}
| protected string getEmptyChildrenExpression ( ) |
protected function getEmptyChildrenExpression(): string
{
return "''";
}
| public array getHierarchy ( string $name ) | ||
| $name | string | |
public function getHierarchy(string $name): array
{
$baseOuterQuery = (new Query($this->database))->select(['item.*', 'parent_of.children']);
$cteSelectRelationQuery = (new Query($this->database))
->select(['parent', new Expression($this->getTrimConcatChildrenExpression())])
->from("$this->childrenTableName AS item_child_recursive")
->innerJoin('parent_of', [
'item_child_recursive.child' => new Expression('{{parent_of}}.[[child_name]]'),
]);
/** @infection-ignore-all FalseValue, union */
$cteSelectItemQuery = (new Query($this->database))
->select(['name', new Expression($this->getEmptyChildrenExpression())])
->from($this->tableName)
->where(['name' => $name])
->union($cteSelectRelationQuery, all: true);
/** @infection-ignore-all FalseValue, recursive */
$outerQuery = $baseOuterQuery
->withQuery($cteSelectItemQuery, 'parent_of(child_name, children)', recursive: true)
->from('parent_of')
->leftJoin(
['item' => $this->tableName],
['item.name' => new Expression('{{parent_of}}.[[child_name]]')],
);
/** @psalm-var Hierarchy */
return $outerQuery->all();
}
| public array getParentRows ( string $name ) | ||
| $name | string | |
public function getParentRows(string $name): array
{
$baseOuterQuery = (new Query($this->database))->select('item.*')->where(['!=', 'item.name', $name]);
/** @psalm-var RawItem[] */
return $this->getRowsCommand($name, baseOuterQuery: $baseOuterQuery)->queryAll();
}
Defined in: Yiisoft\Rbac\Db\ItemTreeTraversal\CteItemTreeTraversal::getTrimConcatChildrenExpression()
| protected string getTrimConcatChildrenExpression ( ) |
protected function getTrimConcatChildrenExpression(): string
{
return "TRIM('$this->namesSeparator' FROM CONCAT(children, '$this->namesSeparator', " .
'item_child_recursive.child))';
}
| public boolean hasChild ( string $parentName, string $childName ) | ||
| $parentName | string | |
| $childName | string | |
public function hasChild(string $parentName, string $childName): bool
{
/**
* @infection-ignore-all
* - ArrayItemRemoval, select.
*/
$baseOuterQuery = (new Query($this->database))
->select([new Expression('1 AS item_child_exists')])
->andWhere(['item.name' => $childName]);
/** @psalm-var array<0, 1>|false $result */
$result = $this
->getRowsCommand($parentName, baseOuterQuery: $baseOuterQuery, areParents: false)
->queryScalar();
return $result !== false;
}
Signup or Login in order to comment.