protected function load() { //@TODO cache if (!($dataItems = (new Query())->from($this->itemsTable)->orderBy(['order_index' => SORT_DESC])->indexBy('name')->all($this->connection))) { throw new RBACException('Items is empty.'); } static::$items = $dataItems; $alias = Query::alias($this->rolesTable, $this->rolesTable); //@TODO cache if (!($dataRolesItems = (new Query())->select(SelectBuilder::selects([['roles' => ['name', 'type', 'description', 'data']], ['access_items' => ['name', 'type', 'description', 'data'], 'items']]))->from($this->rolesItemsTable)->innerJoin($this->itemsTable, "{$this->rolesItemsTable}.item = {$this->itemsTable}.name")->innerJoin($this->rolesTable, "{$this->rolesItemsTable}.role = {$alias}.name")->andWhere(["{$alias}.[[type]]" => RBACInterface::TYPE_ROLE])->orderBy(["{$alias}.[[order_index]]" => SORT_DESC])->asSubattributes()->all($this->connection))) { return; } $result = []; foreach ($dataRolesItems as $value) { if (isset($result[$value['name']])) { $result[$value['name']]['items'] = array_merge($result[$value['name']]['items'], (array) $value['items']['name']); continue; } $value['items'] = [$value['items']['name']]; $result[$value['name']] = $value; } static::$items = ArrayHelper::toType(ArrayHelper::filterRecursive($result + static::$items, function ($value, $key) { return !in_array($key, ['name'], true); })); }
/** * @param mixed $rows * @param ConnectionInterface $connection * @return array */ public function typeCast($rows, ConnectionInterface $connection = null) { if (isset($connection)) { $this->setConnection($connection); } $connection = $this->getConnection(); if ($connection->typeCast) { $rows = is_array($rows) ? ArrayHelper::toType($rows) : Helper::toType($rows); } return $rows; }
/** * @inheritdoc */ public static function populateRecord($record, $row, ConnectionInterface $connection = null) { if (!isset($connection)) { $connection = static::getConnection(); } if ($connection->typeCast) { $columns = static::getTableSchema($connection)->columns; foreach ($row as $name => $value) { if (isset($columns[$name])) { $row[$name] = $columns[$name]->phpTypecast($value); } elseif (is_array($value)) { $row[$name] = ArrayHelper::toType($value); } } } parent::populateRecord($record, $row); }