User login

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

Resolution

Searched words: 
avoid mysql race conditions mysql make sure other queries complete make sure last mysql query complete before proceeding finish last mysql query finish last query before next mysql

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.