I have 2 nodes:
开发者_C百科- Project
- Bug
A project has node references to Bugs
When I delete a Bug, those node references remain as orphaned. What is the best way to clean this up?
UPDATE (HERE IS WHAT I DID)
function dc_project_management_node_delete($node)
{
if ($node->type == 'bug_request')
{
$bug_request_nid = $node->nid;
$milestone_ids = db_query('SELECT entity_id FROM {field_data_field_bugs_requests} WHERE field_bugs_requests_nid = :bug_request_nid', array(':bug_request_nid' => $bug_request_nid))->fetchCol();
$milestones = entity_load('field_collection_item', $milestone_ids);
foreach($milestones as $milestone)
{
for($k=0;$k<count($milestone->field_bugs_requests['und']);$k++)
{
if ($milestone->field_bugs_requests['und'][$k]['nid'] == $bug_request_nid)
{
unset($milestone->field_bugs_requests['und'][$k]);
}
}
entity_save('field_collection_item', $milestone);
}
}
}
If you want referential integrity of sorts (i.e. related bugs are deleted when a project is deleted) you could just do this:
function mymodule_node_delete($node) {
if ($node->type == 'project') {
$bug_nids = array();
foreach ($node->field_bugs[LANGUAGE_NONE] as $bug) {
$bug_nids[] = $bug['nid'];
}
node_delete_multiple($bug_nids);
}
}
精彩评论