User login

Copy taxonomy terms from one node to another

Particularly un-clever code:

function wsf_action_copy_terms($from_nid, $to_nid) {
// can you spell optimizable?  Neither can we. 
// this whole thing could be done with a single mysql query probably
// but would that be the Drupal way?
// probably would've been better to rip out the guts here...  http://api.drupal.org/api/function/taxonomy_node_get_terms/5
  $terms = taxonomy_node_get_terms($from_nid);
// slower but more specific if it ever matters:  $terms = taxonomy_node_get_terms_by_vocabulary($from_nid, place_taxonomy_get_vid());
// adapted from http://api.drupal.org/api/function/taxonomy_node_save/5
// 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);
  }
}

For more, see:
http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html

Resolution

Searched words: 
insert INTO IGNORE mysql insert if doesn't exist

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.