Closed (won't fix)
Project:
Views (for Drupal 7)
Version:
5.x-1.6
Component:
Code
Priority:
Normal
Category:
Bug report
Assigned:
Unassigned
Reporter:
Created:
29 Aug 2007 at 18:56 UTC
Updated:
15 Mar 2010 at 18:05 UTC
Hi, I wanted to make alphabetic listing based on the node title of my CCK node.type 'kniha'. But I've got this SQL error:
user warning: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(node.title, 1) FROM node node WHERE (node.status = '1') AND (node.type IN ('kn' at line 1 query: pager_query SELECT LEFT(node.title, 1) AS letter, count(node.nid) AS num_nodes, node.LEFT(node.title, 1) AS letter_LEFT(node.title, 1) FROM node node WHERE (node.status = '1') AND (node.type IN ('kniha')) GROUP BY LEFT(node.title, 1), letter_LEFT(node.title, 1) ORDER BY letter_LEFT(node.title, 1) ASC LIMIT 0, 50
I've compare views_handler_arg_nodeletter with views_handler_arg_taxletter (which has been working fine) and then I've made these corrections in nodeletter around line 515:
function views_handler_arg_nodeletter($op, &$query, $argtype, $arg = '') {
static $field = NULL;
switch($op) {
case 'summary':
$len = intval($arg);
//$field = $fieldinfo['field'] = ($len <= 0 ? "node.title" : $fieldinfo['field'] = "LEFT(node.title, $len)");
// Why is $fieldinfo['field'] twice here?
$field = $fieldinfo['field'] = ($len <= 0 ? "node.title" : "LEFT(node.title, $len)");
$fieldinfo['fieldname'] = 'letter';
return $fieldinfo;
break;
case 'sort':
//$query->add_orderby('node', $field, $argtype, 'letter');
//This solves the SQL error
$query->add_orderby('', $field, $argtype, 'letter');
break;
These corrections solves my problem.
Here is my exported view:
$view = new stdClass();
$view->name = 'titles';
$view->description = 'All titles sorted alphabetically.';
$view->access = array (
);
$view->view_args_php = '';
$view->page = TRUE;
$view->page_title = 'Tituly';
$view->page_header = '';
$view->page_header_format = '1';
$view->page_footer = '';
$view->page_footer_format = '1';
$view->page_empty = '';
$view->page_empty_format = '1';
$view->page_type = 'list';
$view->url = 'authors';
$view->use_pager = TRUE;
$view->nodes_per_page = '50';
$view->sort = array (
);
$view->argument = array (
array (
'type' => 'nodeletter',
'argdefault' => '4',
'title' => '%1',
'options' => '1',
'wildcard' => '',
'wildcard_substitution' => '',
),
);
$view->field = array (
array (
'tablename' => 'node',
'field' => 'title',
'label' => '',
'handler' => 'views_handler_field_nodelink',
'options' => 'link',
),
);
$view->filter = array (
array (
'tablename' => 'node',
'field' => 'status',
'operator' => '=',
'options' => '',
'value' => '1',
),
array (
'tablename' => 'node',
'field' => 'type',
'operator' => 'OR',
'options' => '',
'value' => array (
0 => 'kniha',
),
),
);
$view->exposed_filter = array (
);
$view->requires = array(node);
$views[$view->name] = $view;
Comments
Comment #1
merlinofchaos commentedHmm. Odd that the code is like that. Your changes definitely appear correct.
Comment #2
karens commentedGetting the same or a similar error and it looks like something broken in the views_handler_arg_nodeletter argument code. I create a simple view with the node title as the only field and node title as the only argument, no other fields, or sorts or filters, and get the following error. Notice that 'node.' is getting prefixed to the node title field making it 'node.node.title' -- the same part of the query that is getting mangled in the example above.
The posted fix doesn't help my situation, still playing around trying to figure out exactly what is broken here...
Comment #3
karens commentedA little more info -- my simple example above creates broken sql only if 'summary unsorted', 'summary sorted ascending' or 'summary sorted descending' is selected, 'summary sorted as view' works fine as do all views that do not add summaries. The only difference between the summaries that don't work and the one that does is that $self-sort is set on the broken summaries and not on the one that works right. I'm stuck at this point trying to figure out why the setting of $self-sort is adding an unwanted prefix to the field when this particular argument is used.
Comment #4
karens commentedShould change the status...
Anyway, it turns out the difference between the summaries that use $self-sort and the one that does not is that the queries that use $self-sort add an alias of 'letter' to the query while the 'summary sorted as view' gets created a different way which does not set the alias. The summaries then run through add_orderby() where an alias is prefixed to our summaries and a field with that alias is added to the view.
The summary that works adds no alias when run through this function, which for some reason creates an alias of 'node_changed', which works correctly, while the 'letter' alias does not, so somewhere in here is where the query is getting garbled.
Something in this code is just not handling aliases right, but I'm not sure I know what all might be affected by changing this code, so I hesitate to suggest a fix. My workaround for now is to use 'summary sorted as view' instead of the broken summaries since that summary does not add a broken field to the query. I can get it to sort right by adding Node:Title to the sort, so that 'summary sorted as view' has the sorting I originally wanted.
Comment #5
esmerel commentedNobody's touched this issue in a year and a half, and nobody's working on 1.x, I'm won't fixing this.