I currently have a setup where i have sauces and recipes. The recipes node type has a node reference to sauce node type. If the site owners delete a sauce then the recipe is no longer useful as an active node and should be deleted. However i cant find a way to accomplish this. Should it be in rules?? The closest related issued i found on this forum is (http://drupal.org/node/750682) but am unsure how i would call that function??

Comments

d3str0y’s picture

I've been attempting to do the same thing. As far as I can tell, you need to create your own custom module. I haven't had any luck getting it working quite yet however.

Check out this blog post: http://madeout.blogspot.com/2009/06/drupal-nodereference-couple-of-bonus...

function mymodule_nodeapi(&$node, $op, $params = NULL, $page = NULL) {
  switch ($op) {
    case 'delete':
      if ($node->type == 'content_type_referenced') {
        $result = db_query("SELECT nid FROM {content_type_referrer} r WHERE 
          r.field_content_reference_nid = %d", $node->nid);
        while ($referrer = db_fetch_object($result)) {
          node_delete($referrer->nid);
        }
      }
    break;
  }
}
yaworsk’s picture

agreed, custom module is the way to go.

d3str0y, at a quick glance, your code looks good, what issues are you having with it? have you confirmed your database names and tables?

d3str0y’s picture

I've ended up implementing solutions from this post, "referential integrity": http://drupal.org/node/83929

There are other interesting suggestions here: http://groups.drupal.org/node/155384
such as:
Views Bulk Operations(VBO) + Rules
Sandbox project: http://drupal.org/sandbox/modulewallah/1136472
http://drupal.org/project/cck_referential_integrity (will not delete nodes but instead sets them to NULL)

tajindersingh’s picture

The hook_nodeapi code will fail in case user deleting the referred node [e.g. Article] doesn't have delete access on referring node [e.g. Article Review] as node_delete function first checks for node_access('delete', $node);

The other solution is to set referring node's referred nid field [e.g. content_type_journax_response] to NULL as below on delete:

function mymodule_nodeapi(&$node, $op, $params = NULL, $page = NULL) {
  switch ($op) {
    case 'delete':
      if ($node->type == 'article') {
        db_query("UPDATE {content_type_review} SET field_review_for_nid = NULL WHERE field_review_for_nid  = %d", $node->nid);
      }
    break;
  }
}

Then further using hook_cron, delete all the referring nodes [Reviews] having referred nid [Article NID] as NULL.

function mymodule_cron() {
  $orphan_reviews = db_query("SELECT nid FROM {content_type_review} WHERE field_review_for_nid IS NULL");
  
  while ($orphan_review = db_fetch_object($orphan_reviews)) {
    node_delete($orphan_review->nid);
  }
}

Best Regards,

Tajinder Singh Namdhari
https://TajinderSinghNamdhari.com

longtom’s picture

https://drupal.org/project/crossclone does some of this, having settings to delete 'downstream' or 'upstream' dependencies.