protected function SqlGroupGraphStorage::loadGroupMapping in Subgroup (Graph) 1.0.x
Fetch the records from graph for the provided group and cache them.
This is mostly done for performance reasons. When having lots of groups, getting/checking the ancestors or descendants in separate queries is a lot slower.
1 call to SqlGroupGraphStorage::loadGroupMapping()
- SqlGroupGraphStorage::loadMap in src/
Graph/ SqlGroupGraphStorage.php - Load the ancestry mapping for a group if it isn't loaded already.
File
- src/
Graph/ SqlGroupGraphStorage.php, line 108
Class
- SqlGroupGraphStorage
- SQL based storage of the group relationship graph.
Namespace
Drupal\ggroup\GraphCode
protected function loadGroupMapping($gid) {
$cid = "ggroup_graph_map:{$gid}";
if ($cache = \Drupal::cache()
->get($cid)) {
$mapping = $cache->data;
}
else {
$query = $this->connection
->select('group_graph', 'gg')
->fields('gg', [
'start_vertex',
'end_vertex',
]);
// This Or Group is identical to the one used in the query below.
$or_group = $query
->orConditionGroup();
$or_group
->condition('gg.start_vertex', $gid)
->condition('gg.end_vertex', $gid);
// Add the Or condition Group.
$query
->condition($or_group);
$mapping['descendants'] = $query
->execute()
->fetchAll(\PDO::FETCH_COLUMN | \PDO::FETCH_GROUP);
$mapping['ancestors'] = $query
->execute()
->fetchAll(\PDO::FETCH_COLUMN | \PDO::FETCH_GROUP, 1);
// Now load direct relatives.
$query = $this->connection
->select('group_graph', 'gg')
->fields('gg', [
'start_vertex',
'end_vertex',
]);
// Add conditions to restrict this to direct descendants for this group.
$query
->condition('hops', 0)
->condition($or_group);
$mapping['directDescendants'] = $query
->execute()
->fetchAll(\PDO::FETCH_COLUMN | \PDO::FETCH_GROUP);
$mapping['directAncestors'] = $query
->execute()
->fetchAll(\PDO::FETCH_COLUMN | \PDO::FETCH_GROUP, 1);
// Descendants and Ancestors contain direct relatives as well.
// Combine ancestors and descendants to get a full list of all of the
// groups returned to tag this cache with.
$groups = array_merge($mapping['descendants'], $mapping['ancestors']);
$groups[] = $gid;
// Build the cache tags for all of the (deduped) groups.
$cache_tags = [];
$groups = $this
->flattenGroups($groups);
$groups = array_unique($groups, SORT_NUMERIC);
foreach ($groups as $group_id) {
$cache_tags[] = "group:{$group_id}";
}
\Drupal::cache()
->set($cid, $mapping, Cache::PERMANENT, $cache_tags);
}
// Merge relatives with those already set.
$this
->mergeMappings($mapping);
// Indicate that this group ID has been loaded.
$this->loaded[$gid] = TRUE;
}