- Patch #142051 by catch, moonray: static cache for taxonomy_get_parents() and taxonomy_get_children().

merge-requests/26/head
Dries Buytaert 2010-01-08 11:03:24 +00:00
parent c92ddd4cb8
commit 4b8ba4fa2c
1 changed files with 43 additions and 28 deletions

View File

@ -563,6 +563,8 @@ function taxonomy_term_delete($tid) {
function taxonomy_terms_static_reset() { function taxonomy_terms_static_reset() {
drupal_static_reset('taxonomy_term_count_nodes'); drupal_static_reset('taxonomy_term_count_nodes');
drupal_static_reset('taxonomy_get_tree'); drupal_static_reset('taxonomy_get_tree');
drupal_static_reset('taxonomy_get_parents');
drupal_static_reset('taxonomy_get_children');
entity_get_controller('taxonomy_term')->resetCache(); entity_get_controller('taxonomy_term')->resetCache();
} }
@ -610,19 +612,25 @@ function taxonomy_vocabulary_get_names() {
*/ */
function taxonomy_get_parents($tid, $key = 'tid') { function taxonomy_get_parents($tid, $key = 'tid') {
if ($tid) { if ($tid) {
$query = db_select('taxonomy_term_data', 't'); $tids = &drupal_static(__FUNCTION__, array());
$query->join('taxonomy_term_hierarchy', 'h', 'h.parent = t.tid'); if (isset($tids[$key][$tid])) {
$result = $query $parents = $tids[$key][$tid];
->addTag('translatable') }
->addTag('term_access') else {
->fields('t') $query = db_select('taxonomy_term_data', 't');
->condition('h.tid', $tid) $query->join('taxonomy_term_hierarchy', 'h', 'h.parent = t.tid');
->orderBy('weight') $result = $query
->orderBy('name') ->addTag('translatable')
->execute(); ->addTag('term_access')
$parents = array(); ->fields('t')
foreach ($result as $parent) { ->condition('h.tid', $tid)
$parents[$parent->$key] = $parent; ->orderBy('weight')
->orderBy('name')
->execute();
$parents = array();
foreach ($result as $parent) {
$parents[$parent->$key] = $parent;
}
} }
return $parents; return $parents;
} }
@ -660,23 +668,30 @@ function taxonomy_get_parents_all($tid) {
* Find all children of a term ID. * Find all children of a term ID.
*/ */
function taxonomy_get_children($tid, $vid = 0, $key = 'tid') { function taxonomy_get_children($tid, $vid = 0, $key = 'tid') {
$query = db_select('taxonomy_term_data', 't'); $tids = &drupal_static(__FUNCTION__, array());
$query->join('taxonomy_term_hierarchy', 'h', 'h.tid = t.tid'); if (isset($tids[$vid][$tid])) {
$query $children = $tids[$vid][$tid];
->addTag('translatable')
->addTag('term_access')
->fields('t')
->condition('parent', $tid)
->orderBy('weight')
->orderBy('name');
if ($vid) {
$query->condition('t.vid', $vid);
} }
$result = $query->execute(); else {
$query = db_select('taxonomy_term_data', 't');
$query->join('taxonomy_term_hierarchy', 'h', 'h.tid = t.tid');
$query
->addTag('translatable')
->addTag('term_access')
->fields('t')
->condition('parent', $tid)
->orderBy('weight')
->orderBy('name');
if ($vid) {
$query->condition('t.vid', $vid);
}
$result = $query->execute();
$children = array(); $children = array();
foreach ($result as $term) { foreach ($result as $term) {
$children[$term->$key] = $term; $children[$term->$key] = $term;
}
$tids[$vid][$tid] = $children;
} }
return $children; return $children;
} }