Community & Support

taxonomy_node_save() not creating the term<->node relation

Hi there,

I'm trying to do a term freetagging insert on node insertion. When somebody creates a node i want to create tags from some node variables. So i have the following code in the hook_insert() function:

    $nid = $node->nid;
    $vid = '6';
    $terms['tags'][$vid] = ''.$node->var1.','.$node->var2.'';
    taxonomy_node_save($nid, $terms);

Somehow it is creating the terms, i can find them in the term_data table. But the term-node relation isn't created in the tern_node table.
I can't locate the problem. I searched the forums for a solution, but i can't find any.

I hope somebody can give me a hint or a solution.

Thanx in advance

daflow

Comments

I tried an other function called taxonomy_node_delete(), en it won't delete the relations made in term_node. I searched for issues on the term_node table. And i found items on the double Primary index warning in mysql. But i don't know if this is the cause for my problems. Somehow the taxonomy functions won't do any actions on the term_node table
Has somebody come along the same problems with these taxonomy functions. Or am i doing things the wrong way.

Thanx for your reaction

Grtz daflow

taxonomy_node_save()

I am trying to do exactly the same thing, and I believe what is happening is that the taxonomy_node_save call is in fact updating the term_node table, however nodeapi subsequently gets called, which in tern calls taxonomy_node_save() again, but this time $node->taxonomy is not set. The first line in the taxonomy_node_save function deletes any term_node relations and then since taxonomy is not set this time around, the relations never get rebuilt.

Unfortunately, I haven't found a way around this yet.

Ron.

taxonomy_node_delete

Did you figure this one out in the end?

I was having a similar problem which I believe was to do with the first line of the "taxonomy_node_save" function.

function taxonomy_node_save($nid, $terms) {
  taxonomy_node_delete($nid);

I was saving the taxonomy data, all node relations were fine but then the other taxonomy terms on the node (applied using admin/categories) were calling this function again, wiping out my taxonomy terms and then putting their own ones in.

I found a different solution to my problem (I used the taxonomy module to add the inputs I wanted and then form_alter to change them to what I needed).

I'm not that familiar with Drupal best practices but I guess you could do something like

function taxonomy_node_save($nid, $terms, $delete_terms = true) {
if ($delete_terms) {
taxonomy_node_delete($nid);
}

And then call

taxonomy_node_save($nid, $terms, false);

Hope that helps!

Sunil
http://blog.suniljolly.com

$node->taxonomy at your pleasure...

taxonomy_node_save() is called by the taxonomy module when you insert/update a node. As rjerome pointed out, it will delete any "node<->terms" you saved in your hook_insert() and hook_update().

The good news is - there is an even easier solution, and it really gets to the gold of the hook system: the single $node variable evolves as it passes through hook_validate, hook_submit, and hook_insert/hook_update.

So if you want to manually change the taxonomy before the node is saved to the database, just change the $node->taxonomy array in any of the hooks. So, in response to the original example:

<?php
// Add auto-tags on node insert
function mymodule_insert($node) {
  
$vid = 6;
  
$tagstring = $node->var1.','.$node->var2;
  
$node->taxonomy['tags'] = array($vid => $tagstring);
}
?>

Another example...

<?php
// Add a new term on node insert
function mymodule_insert($node) {
 
$vid = 2;   // vocabulary ID is hard coded for this example
 
$autoterm = array(
   
'name' => $node->title// or whatever you want the auto-term to be named
   
'parent' => 0,
   
'vid' => $vid,
  );
 
taxonomy_save_term($autoterm);
 
$tid = (int) db_result(db_query('SELECT MAX(tid) FROM {term_data} WHERE vid = %d', $vid));
 
$node->taxonomy[$vid][$tid] = $tid;
}
?>

The hook system is called before nodeapi in order - hook_submit then nodeapi(submit), hook_insert then nodeapi(insert). So you should be able to get your $node edited anyway you like before a module runs its nodeapi on it.

hook_submit works

I had a very similar problem in a module handling a node; my goal was to create a node, a term with the same name and associate them.
Writing all the code in hook_insert wasn't affecting the term_node table (actually it was, but the relation got deleted immediately afterwards).
The following worked for me:

function sperimentazione_submit(&$node) {

  // I create a term in a specific vocabulary with the same name as the node
  $edit = array('name' => $node->title, 'description' => $node->body, 'weight' => "0", 'vid' => _sperimentazione_get_vid());
  taxonomy_save_term($edit);
  $sperimentazione_term_id = $edit['tid'];

  // associate node and term
  $node->taxonomy = array(_sperimentazione_get_vid() => array($sperimentazione_term_id => $sperimentazione_term_id));
}

Davide

nobody click here