By JohnWoltman on
With Drupal 7 (HEAD), I am using the TableSort extender in a custom module to output some data in a sortable table. The problem is that if I add an expression (with $query->addExpression()) the query does not respect the order in which I added the expression. If I have 3 columns (1 real column, 1 expression, and another real column), it always puts the expression last, under the wrong heading in the table.
No matter what order expressions and fields are added to a select query, the expressions always come after all the fields. Am I doing something wrong, or should I be filing a bug report?
Here's the code:
// $headers for theme_table and TableSort extender
$headers = array(
array( 'data' => 'Manufacturer', 'field' => 'company_name', 'sort' => 'asc' ), // FIRST column
array( 'data' => 'Item', 'field' => 'itemlink'), // SECOND column
array( 'data' => 'Abbr.', 'field' => 'abbreviation') // THIRD column
);
// Set up the query
$query = db_select('battery_model_list_view');
$query = $query->extend('TableSort');
$query = $query->orderByHeader($headers);
$query->addField('battery_model_list_view', 'company_name'); // FIRST db column
$query->addExpression("'<a href=\"/batt/details/' || batt_model_id ||'\">' || model || '</a>'", 'itemlink'); // SECOND db column
$query->addField('battery_model_list_view', 'abbreviation'); // THIRD db column
// Execute the query
$result = $query->execute();
$rows = $result->fetchAll(PDO::FETCH_ASSOC);
// Create $table variables for theme_table()
$table = array(
'header' => $headers,
'rows' => $rows,
'caption' => null,
'attributes' => array(),
'colgroups' => array(),
'sticky' => null,
'empty' => null,
);
$out = "<p>The following is a list of items. Click the model to see details of that model</p>";
$out .= theme_table($table);
return $out;
Comments
A bump
Just a bump to get exposure for this, before I file a bug report. It seems like a large-ish (though not critical) issue to me.
Did you ever file a ticket
Did you ever file a ticket about this one? I just ran into it myself when trying to use PDO:FETCH_CLASSTYPE and ended up having to do a kind of ugly hack of adding each real column through addExpression instead of addField just to get it working right.
Just ran into this as well.
Just ran into this as well. Hrmm, now to hack expressions or go back to db_query.