3

I have a query, with a date field, let's say it is aliased as enabled. And joined table with field enabled_for_website. In my query alter, I want to substitute global enabled with date specific to website user is viewing (domain access, but it doesn't really matter here).

I tried simplistic approach:

function MYMODULE_query_MYTAG_alter($products_query) {
  $products_query->join('website_has_product', 'wp', 'p.idProduct = wp.idProduct');
  $products_query->join('website', 'w', 'wp.idWebsite = w.idWebsite');
  global $user;
  if ($user->uid == 1) {
    $products_query->addField('wp', 'website_enabled', 'enabled');
  }
  $products_query->condition('w.WebsiteName', /*where are we */, '=');
}

I'm getting expected error:

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'enabled' in order clause is ambiguous

Is there a way to remove other column? SelectQuery::$fields is protected, and I see no method to remove / substitute fields in it. I would prefer to avoid extending any classes.

1 Answer 1

5

SelectQuery::getFields() returns by reference, so something like this should do it:

$fields =& $query->getFields();
$found_index = FALSE;
foreach ($fields as $key => $val) {
  // Check what's in the field, note the index if it matches the one to remove
}

if ($found_index !== FALSE) {
  unset($fields[$found_index];
}
1
  • Pure unset will, of course, be a bad idea. Unset with replacement worked for me, thanks. Somehow I missed it returns by reference, my mistake; need to read more carefully.
    – Mołot
    Commented Oct 9, 2014 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.