I have a customized version of the nodereference module that allows the user to be able to dynamically attach more than 3 node references to each node without requiring a page save and edit. I have been able to get the module working except for the fact that drupal seems to overwrite the values inserted into the database for that added node reference. That is, Drupal is storing the inital value of $delta and not reading the "content_field_field_name" for recently added values.
I tried disabling the cache by doing $GLOBALS['conf']['cache'] = FALSE; and cache_clear_all('cck_dynamic_nodereference', 'cache', TRUE); in hook_init() function. Inspite of adding this drupal still overwrites the ajax call added values. The only way that I have been able to get drupal to persist the ajax call added values is by running devel module's "Empty Cache" before doing Preview/Submit on the node.
This is the function that handles the ajax requests:
function cck_dynamic_nodereference_init() {
$GLOBALS['conf']['cache'] = FALSE;
cache_clear_all('cck_dynamic_nodereference', 'cache', TRUE);
}
function cck_dynamic_nodereference_response() {
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 30 Jul 1984 05:00:00 GMT");
$nid = $_REQUEST['nid'];
$vid = $_REQUEST['vid'];
$delta = $_REQUEST['delta'];
$field_value = $_REQUEST['field_value'];
$field_name = $_REQUEST['field_name'];
$field_column_name = 'field_' . $field_name . '_nid';
$field_table_name = 'content_field_' . $field_name;
db_query("insert into {". $field_table_name ."} (vid, delta, nid, ". $field_column_name .") values (%d, %d, %d, %d)", $vid, $delta, $nid, $field_value);
$ajaxOutput = "some output from here.... Success !!! with values--> $nid, $vid and $field_table_name and $field_column_name";
$ajaxOutput = drupal_to_js($ajaxOutput);
echo $ajaxOutput;
}
The code for the ajax call
Drupal.CCKDynamicNodereference.commitNewNodeReference = function(delta) {
nid = $('#node_nid').html();
vid = $('#node_vid').html();
field_value = $('#cck_dnr select:eq('+delta+')').val();
previous_select_name = $('#cck_dnr select:eq(0)').attr('name');
previous_select_name_end = previous_select_name.lastIndexOf("0");
previous_select_name_end = previous_select_name_end - 1;
field_name = previous_select_name.substring(6, previous_select_name_end);
$.ajax({
method: "get",url: "/cck_dynamic_nodereference/response/",
data: "vid="+vid+"&delta="+delta+"&nid="+nid+"&field_value="+field_value+"&field_name="+field_name,
dataType: 'json',
success: function(data){
console.log('data returned successfully '+data);
}
});
}
I have spent almost 2 weeks on this module and would hate to scrap it only because of this caching issue, therefore any help is appreciated.
Comments
Comment #1
janwari commentedJust in case if someone is facing a similar situation, the crude work around was to run the following SQL queries before updating the database.
db_query("TRUNCATE TABLE {cache}");
db_query("TRUNCATE TABLE {cache_content}");
db_query("TRUNCATE TABLE {cache_filter}");
db_query("TRUNCATE TABLE {cache_menu}");
db_query("TRUNCATE TABLE {cache_page}");
Comment #2
gcassie commentedHave you tried just targeting cache_content? Beyond that, could you target the cid column and just delete those rows? You should have the nid and vid available to do so.
Just a thought.
Comment #3
karens commentedClosing old issues. None of us is using the D5 version any more, so hard to provide any support. Sorry.