How can I substitute field in hook_query_alter? 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-&gt;join('website_has_product', 'wp', 'p.idProduct = wp.idProduct');
  $products_query-&gt;join('website', 'w', 'wp.idWebsite = w.idWebsite');
  global $user;
  if ($user-&gt;uid == 1) {
    $products_query-&gt;addField('wp', 'website_enabled', 'enabled');
  }
  $products_query-&gt;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.
SelectQuery::getFields() returns by reference, so something like this should do it:

$fields =&amp; $query-&gt;getFields();
$found_index = FALSE;
foreach ($fields as $key =&gt; $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];
}