Can't use taxonomy_node_get_terms immediately after terms inserted
The short version is in the title -- use your own query (or the get terms by vocab one which doesn't use a cache technique of any sort, no static variable).
The rest of the post is how NOT to do it when you're pressed for time and making stupid time-consuming mistakes.
let's transfer places
Action made in the same turn (but definitely after) an Action space (organic group) does this:
* begin: 647
terms: Array ( [5] => Array ( [20] => USA [40] => Massachusetts [60] => Natick [70] => 01760 ) )
* done
* trace not available: cache_refresh_all Array ( )
* trace not available: cache_refresh_all Array ( [0] => [1] => cache_page )
* Your Action space has been created.
* From: 647 - To: 648
* race avoiding
*
Array
(
)
* trace not available: cache_refresh_all Array ( )
* trace not available: cache_refresh_all Array ( [0] => [1] => cache_page )
* Your Action has been created.
Action made immediately thereafter, it works.
* From: 647 - To: 649
*
Array
(
[217] => stdClass Object
(
[tid] => 217
[vid] => 5
[name] => USA
[description] =>
[weight] => 20
[language] =>
[trid] => 0
)
[56] => stdClass Object
(
[tid] => 56
[vid] => 5
[name] => Massachusetts
[description] =>
[weight] => 40
[language] =>
[trid] => 0
)
[57] => stdClass Object
(
[tid] => 57
[vid] => 5
[name] => Natick
[description] =>
[weight] => 60
[language] =>
[trid] => 0
)
[58] => stdClass Object
(
[tid] => 58
[vid] => 5
[name] => 01760
[description] =>
[weight] => 70
[language] =>
[trid] => 0
)
)
* trace not available: cache_refresh_all Array ( )
* trace not available: cache_refresh_all Array ( [0] => [1] => cache_page )
* Your Action has been created.
I am so lost:
begin: 656 terms: Array ( [5] => Array ( [20] => USA [40] => Massachusetts [60] => Natick [70] => 01760 ) )
done
trace not available: cache_refresh_all Array ( )
trace not available: cache_refresh_all Array ( [0] => [1] => cache_page )
Your Action space has been created.
From: 656 - To: 657
race avoiding
Array
(
[tid] => stdClass Object
(
[tid] => 58
)
)
trace not available: cache_refresh_all Array ( )
trace not available: cache_refresh_all Array ( [0] => [1] => cache_page )
Your Action has been created.
01760 is the zip code.. but why?
$result = db_query('SELECT tid FROM {term_node} WHERE nid = %d', $from_nid);
$terms = array();
while ($term = db_fetch_object($result)) {
$terms['tid'] = $term;
}
Do a quick test:
$from_nid = 656;
$result = db_query('SELECT tid FROM {term_node} WHERE nid = %d', $from_nid);
$terms = array();
while ($term = db_fetch_object($result)) {
$terms['tid'] = $term;
}
drupal_set_message('
' . print_r($terms, TRUE) . '
');
OK, so I just have a bad SQL query.
This works:
SELECT *
FROM `term_node`
WHERE `nid` =656
ORDER BY `term_node`.`tid` ASC
LIMIT 0 , 30
This does too, so it's not my SQL query:
SELECT tid FROM term_node WHERE nid = 656;
Oh, bloody hell it's the 'tid' tacked onto terms.
function wsf_action_copy_terms($from_nid, $to_nid, $avoid_race = FALSE) {
drupal_set_message('From: ' . $from_nid . ' - To: ' . $to_nid);
// agaric-ben @TODO optimize;
// see http://agaricdesign.com/note/copy-taxonomy-terms-from-one-node-another
if ($avoid_race) {
drupal_set_message('race avoiding');
// we have to avoid a pseudo race condition in which use of Drupal's
// taxonomy_node_get_terms (which caches) will fail to return anything
$result = db_query('SELECT tid FROM {term_node} WHERE nid = %d', $from_nid);
$terms = array();
while ($term = db_fetch_object($result)) {
$terms[$term->tid] = $term;
}
} else {
$terms = taxonomy_node_get_terms($from_nid);
}
// WARNING: INSERT IGNORE is MySQL only
foreach ($terms as $tid => $term) {
db_query('INSERT IGNORE INTO {term_node} (nid, tid) VALUES (%d, %d)', $to_nid, $tid);
}
}
was looking into lock tables
http://dev.mysql.com/doc/refman/5.0/en/flush.html
all I had to do was circumvent Drupal's internal static variable for that, which I did-- but I also screwed up by leaving $nid in the query instead of $from_nid, and then screwed up the query again
did not have that time to burn
Comments
Post new comment