This is another follow up of: #847682: Allow JOINS in ctools exports

CTools use the key from the schema to delete items from a table.

Now, the keys can be taken from another table. This make not possible to delete this kind of objects.

Here is an initial code for ctools_export_crud_delete()

function ctools_export_crud_delete($table, $object) {
  $schema = ctools_export_get_schema($table);
  $export = $schema['export'];

  if (!empty($export['delete callback']) && function_exists($export['delete callback'])) {
    return $export['delete callback']($object);
  }
  else {
    // If we were sent an object, get the export key from it. Otherwise
    // assume we were sent the export key.
    $value = is_object($object) ? $object->{$export['key']} : $object;
    $join_query = '';
    if (!empty($export['key in table']) && $export['key in table'] != $table) {
      foreach ($schema['join'] as $join) {
        if ($join['table'] == $export['key in table']) {
          $join_query = ", {$join['table']} t__1";
          $join_where = "AND t__0.{$join['left_key']} = t__1.{$join['right_key']}";
          if (!empty($join['extras'])) {
            $join_where .= ' ' . $join['extras'];
          }
          break;
        }
      }
    }
    db_query("DELETE t__0 FROM {$table} t__0 $join_query WHERE " . $export['key'] . " = '%s' $join_where", $value);
  }
}

I don0't know if this works fine in PostgreSQL, and if we should enabled to delete data for the tables on the inner joins. In my opinion, maybe we could call to another function to take care about those data.

More references about deleting tables using inner joins: http://dev.mysql.com/doc/refman/5.1/en/delete.html

CommentFileSizeAuthor
#2 ctools-853114-docs.patch951 bytesdagmar

Comments

merlinofchaos’s picture

Hm. I don't have a test environment for this feature, so I'll go ahead and do this if you can verify it works on your stuff.

dagmar’s picture

Status: Active » Needs review
StatusFileSize
new951 bytes

Ok, after some research, I think is better that if a module uses joins, it implements a 'delete callback'.

This patch include a bit of documentation about this behavior.

PostgreSQL and MySQL, manage this kind of deletions in different ways, so, in my opinion is more safe do this kind of things in a custom way.

merlinofchaos’s picture

Status: Needs review » Fixed

Rewritten and committed.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.