Search code examples
mysqldrupaldrupal-6cckdelta

Drupal database api delta


I am working on a module which directly updates the data of a set CCK taxonomy checkboxs via ajax.

Everything is working with the exception of the saving to database. Using the following code:

$data = array(
    'vid' => $nid,
    'nid' => $nid,
    'field_grouping_value' => $tid
);
drupal_write_record('content_field_grouping', $data);

I get error messages which complain I am dublicating the value of the 'delta' field. My problem is the code is attempting to write my new data but the delta field is a joint key which does not auto-increament.

Ordinarily, I would just increment the delta field but Drupal handles it so dynamically I'm worried I will do more harm than good: screen shot of database table with delta field

https://i.sstatic.net/yTwoN.gif">

If nid 3 had another field in the above image, it's delta field will have a value of 2. I then delete the field with nid of 3 and delta of 0. Instead of being left with the delta field of values 1 and 2, everything gets reset to 0 and 1

Just so I don't damage my site, I wonder if anyone can advice how I can resolve this issue and get Drupal to dynamically deal with the delta field ..... or is it unnecessary ????


Solution

  • You'll be perfectly safe just using the next available delta, CCK re-does these every time a node with that field is edited and saved anyway (which is why you're seeing the adjustments when you remove fields). Something like this should work:

    $next_delta = db_result(db_query('SELECT MAX(delta) + 1 AS del FROM {content_field_grouping} WHERE vid = %d AND nid = %d', $nid, $nid));
    
    $data = array(
      'vid' => $nid,
      'nid' => $nid,
      'field_grouping_value' => $tid,
      'delta' => $next_delta
    );
    drupal_write_record('content_field_grouping', $data);