I want to get 1 column from a table in Drupal as 2 aliases. Something like this but with Drupal's query methods.:
SELECT name AS label, name AS value FROM node WHERE 1
This Drupal code doesn't set the right alias:
$query = db_select('node', 'node');
$query->fields('node', array('label' => 'name','value' => 'name'));
It returns something like: [name] => Science [node_name] => Science
Is there any way to set the alias?
The 'fields' method does not allow you to set aliases. If you look at the docs, the second argument for fields is an indexed array, so numbers only.
http://api.drupal.org/api/drupal/includes--database--select.inc/function/SelectQuery::fields/7
If you need aliases, then you need to use 'addField'.
http://api.drupal.org/api/drupal/includes--database--select.inc/function/SelectQuery::addField/7
$query = db_select('node', 'n');
$query->addField('n', 'name', 'label');
$query->addField('n', 'name', 'value');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With