Hello,
I've been working on developing a module that connects locations and nodes using the taxonomy and location modules. Anyways, I'm using the hook_nodeapi() and im checking for updates and inserts on my specific node type. Well, I am able to set up the term_data table with my data and i can set up the hierarchy in the term_hierarchy table. Everythings looking fine in my views and the database, but my problem lies in the term_node table. I've created a recursive function that builds the terms and their relationship, then on the base case, I want to insert a relationship between my bottommost term in my hierarchy with the my specified node.
The problem is when I try to make that insert, it never shows up in the database. I've used taxonomy_node_save, db_query and the raw mysql_query, but none of them allow insertion. I'm not getting any errors either. I can manually insert it in phpmyadmin though. One thing though is that as I'm trying to insert, either before or after my insert, a node ($nid) is being linked with another term which is what I want to happen.
So here's what I tried. I went into the vocab edit and checked multiple select, that did nothing. I've echoed out the data I'm trying to insert, and its correct. I've checked the return on db_query and its returning true. I've tried to stop site flow to see if my term_node relation was being inserted before or after the other one (I still can't tell). I've messed with the actual tables primary keys, but I havn't added a new field for an auto_increment key.
My only thoughts are that my function is getting called before the other one and when that one is being called, its probably calling taxonomy_node_save which looks like the first thing it does is delete all other nodes with the same nid.
Does anyone have any suggestions? I'm really stuck here. Once I solve this problem, I'll have an extremely flexible module and I would love to see it working.
Tyler Wright
CreativeFuelDesign.com
Comments
A guess
A guess you have the vocabulary associated with the content (under administer -> categories) and your module name starts with a letter befor the letter 't'. In that case the taxonomy module runs after you module and blanks out the changes you module made.
close
You were right on the naming. I changed my module's name to start with an x and then loaded it and unloaded the old one. I then made my module run the taxonomy_node_save function and checked the database. I noticed that the first insert, the one thats not mine, wasn't there. That tells me that mine is now being called after the taxonomy's. The problem has to be in my insert then because it still won't go in. Heres the block of code I made and that I've been messing with
The commented out portions are the different way's I've been trying to insert. In an example situation, I've created a new node with say nid=58, that creation triggers my hook_nodeapi. I filter it just to make sure its my node type, then I query for the location that was created with it. Once I have that, I go into my recursive function with nid=58 and another param = 0 to set the root of the hierarchy to fill out the term_data and term_hierarchy tables. Each time the function calls itself, it sends in a new parent which was the last tid created in term_data. Once it reaches the base case, it hits the code I posted above. Lets say that the last term created was tid=70, What should happen in the code above is that $nid=58 and $parent=70. When I call my INSERT, it should be putting in a row with $nid=58 and $tid=70. Its for sure getting there as I've popped echos to trace where its going. Its got to be in that insert call.
My term_node table looks like this:
Field Type Collation Attributes Null Default
nid int(10) UNSIGNED No 0
tid int(10) UNSIGNED No 0
The primary key is made up of the nid and the tid and I have an index on tid (I did have it on both, but phpmyadmin was throwing a warning). The Cardinality of the key is 43.
For a second there, I thought it could have been a strict x-lock that the taxonomy was holding, but it allowed me to delete their row with my call to taxonomy_node_save. Its gotta be in that INSERT of mine. Any ideas?
A couple thought
You really should use the taxonomy function and from your description I think I understand the problem. But first a comment on the first INSERT
That should be a call to db_query, like this
I suspect the problem you are having with taxonomy_node_save is I am guessing you are calling it multiple times for the same nid. Instead you need to build up an aray of term id's ($parent in your code) and pass an that array as the second argument. Note you need the array even if only one term id, but if more than one you need to pass them all at once (otherwise new calls to taxonomy_node_save remove the old entries)
I'm doing something similar,
I'm doing something similar, and I have the same problem about taxanomy_node_save running again after my module. I've changed the name of my module to start with an X too and it works, but I really need some kind of explanation why it works, and how to simply stop the taxonomy_node_save from running in a more "cleaner" way.
Idan
Ok, I found out that you
Ok, I found out that you need to set the module weight and then it runs after the taxonomy.
Idan
Excellent Solution
'x' works for me too thank u. but can u explain what is the reason behind this ?