This one is something I really don't know nothing about. Table prefixing is very new for me.

When using Domain Prefix, in a code snippet that is supposed to be somewhere inside a node, if I use db_query("UPDATE {table} ...") in a table that has domain versions, all the versions will be updated? If not, is there a function where I can have all table prefixes to vary while updating, or where I can send the table, the field and the value to be updated?

Thanks!

Comments

agentrickard’s picture

Category: support » feature

OK. In your query, the currently active database table will be used. That is, the {table} assigned to the domain that triggered the code snippet. All tables will not be updated by default.

If you need to update multiple domains at the same time, you would need to change the active prefix string, which should be possible.

A function to do this for you would be very cool. Something like:

function domain_prefix_query($query, $table, $domain_id = NULL, $all = FALSE) {
  global $db_prefix;
  $temp = $db_prefix;
  if ($all) {
    $domains = domain_domains();
    foreach ($domains as $domain) {
      $db_prefix = 'domain_'. $domain['domain_id'];
      if (domain_prefix_table_exists($db_prefix, $table)) {
        db_query($query);
      }
    }
  }
  else {
    if (empty($domain_id)) {
      global $_domain;
      $domain_id = $_domain['domain_id'];
    }
    $db_prefix = 'domain_'. $domain_id;
    if (domain_prefix_table_exists($db_prefix, $table)) {
      db_query($query);
    }
  }
    $db_prefix = $temp;
}

A few warnings, though.

-- The code above will not work if you have a global prefix (such as 'drupal_{table}) on your database. Fixi9ng that would require a preg_replace().

-- The code above is only useful for insert, update, and delete statements.

You would then do this in code:

  $query = "UPDATE {table} SET val = 1";
  domain_prefix_query($query, 'table', NULL, TRUE); // update all domains.  

Testing and a patch would be great.

Vuds’s picture

I'll see that soon, I'm just too occupied with my new site released (and it needs that; but for the moment I'll update the needed table "manually").

Thanks for help!

Vuds’s picture

Just a question, you are speaking of patching, which archive/module should I consider as the API to patch?

Thanks!

agentrickard’s picture

Patch Domain Prefix. The function would go in domain_prefix.module.

agentrickard’s picture

Status: Active » Postponed
agentrickard’s picture

Status: Postponed » Needs work
gunzip’s picture

Thank you for the reply here #356579: Programmatically use drupal functions with domain-prefixed tables.

it's also useful to limit the function to just build the prefix (and not to execute the query too)
so one can use something like

global $db_prefix

$db_prefix = domain_get_prefix();

drupal_execute(...)

as i guess the drupal api use the global variable.
this may go into the module as an exported api.

agentrickard’s picture

The code above, however, will only work for the currently active site, which already changes the $db_prefix value as needed.

gunzip’s picture

well, i had the need to modify content of other domains from another one
so i ended up using this:


function _domain_execute($domain_id, $callback, $arguments) {
  global $db_prefix;
  $saved_prefix = $db_prefix;

  $prefix = domain_prefix_string($domain_id);

  $tables = db_query("SELECT tablename FROM {domain_prefix} WHERE status IN (%d, %d) AND
    domain_id = %d", DOMAIN_PREFIX_CREATE, DOMAIN_PREFIX_COPY, $domain_id);

  while($table = db_fetch_object($tables)) {
    if (domain_prefix_table_exists($prefix, $table->tablename)) {
      $db_prefix[$table->tablename] = $prefix;
    }
  }

  $ret = call_user_func_array($callback, $arguments);
  $db_prefix = $saved_prefix;

  return $ret;
}

so i can temporarly use the tables of the other domain then restore the prefix.

(ie. _domain_execute(24, 'menu_link_save', array('link_path' => 'node/37', 'link_title' => 'link'));

probably there are better ways to do this.

@agentrickard thank you again for the fast replies.

agentrickard’s picture

That's a nice-looking function. I would rename it domain_prefix_execute, since it is meant to be called by external modules and because it belongs in Domain Prefix module.

agentrickard’s picture

Status: Needs work » Closed (won't fix)

5.x is closed to new features. There are ways to do this in 6.x now.