I created a view that that showed in a block the 6 most recent comments mode on the site:

Comment subject - time posted - new/updated

I decided that people on my site don't need to see the comments in the list that they have already read so I created a filter that would filter on "(Node) Node: Has new content" and included new comments in the filter.

I immediately started getting a MySQL 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 '> (1242851091 - 2592001) OR node_comment_statistics.last_comment_timestamp > (12' at line 7 query: SELECT COUNT(*) FROM (SELECT comments.cid AS cid FROM comments comments LEFT JOIN node node_comments ON comments.nid = node_comments.nid LEFT JOIN history node_comments_history_user ON node_comments.nid = node_comments_history_user.nid AND node_comments_history_user.uid = 10 LEFT JOIN comments node_comments__comments ON node_comments.nid = node_comments__comments.nid INNER JOIN node_comment_statistics node_comments__node_comment_statistics ON node_comments.nid = node_comments__node_comment_statistics.nid WHERE (node_comments.status <> 0 OR node_comments.uid = 10 or 1 = 1) AND ((node_comments_history_user.timestamp IS NULL AND (.changed > (1242851091 - 2592001) OR node_comment_statistics.last_comment_timestamp > (1242851091 - 2592001))) OR node_comments_history_user.timestamp < .changed OR node_comments_history_user.timestamp < node_comment_statistics.last_comment_timestamp) ) count_alias in /var/www/vhosts//httpdocs/sites/all/modules/views/includes/view.inc on line 705.

Here is a better formatted version of the query

SELECT
  comments.cid AS cid,
  comments.subject AS comments_subject,
  comments.nid AS comments_nid,
  comments.timestamp AS comments_timestamp,
  node_comments__comments.cid AS node_comments__comments_cid,
  node_comments__comments.nid AS node_comments__comments_nid,
  history_user.timestamp AS history_user_timestamp,
  history_user.nid AS history_user_nid,
  node_comments.created AS node_comments_created,
  node_comments.changed AS node_comments_changed,
  node_comments__node_comment_statistics.last_comment_timestamp AS node_comments__node_comment_statistics_last_comment_timestamp
FROM comments comments
LEFT JOIN node node_comments ON comments.nid = node_comments.nid
LEFT JOIN history history_user ON node_comments.nid = history_user.nid
  AND history_user.uid =10
LEFT JOIN comments node_comments__comments ON node_comments.nid = node_comments__comments.nid
INNER JOIN node_comment_statistics node_comments__node_comment_statistics ON node_comments.nid = node_comments__node_comment_statistics.nid
WHERE
  (
    node_comments.status <>0
    OR node_comments.uid =10
    OR 1 =1
  )
  AND
  (
    (
      history_user.timestamp IS NULL
      AND
      (
        .changed > ( 1242848360 -2592001 ) OR .last_comment_timestamp > ( 1242848360 -2592001 )
      )
    )
    OR history_user.timestamp < .changed
    OR history_user.timestamp < .last_comment_timestamp
  )
  ORDER BY comments_timestamp DESC
  LIMIT 0 , 6

notice in this section of the WHERE clause

AND
  (
    (
      history_user.timestamp IS NULL
      AND
      (
        .changed > ( 1242848360 -2592001 ) OR .last_comment_timestamp > ( 1242848360 -2592001 ) 
      )
    )
    OR history_user.timestamp < .changed 
    OR history_user.timestamp < .last_comment_timestamp 
  )

There are some tables are not named

I managed to fix the query, but I do not know how to fix views to give me the correct query

SELECT
  comments.cid AS cid,
  comments.subject AS comments_subject,
  comments.nid AS comments_nid,
  comments.timestamp AS comments_timestamp,
  node_comments__comments.cid AS node_comments__comments_cid,
  node_comments__comments.nid AS node_comments__comments_nid,
  history_user.timestamp AS history_user_timestamp,
  history_user.nid AS history_user_nid,
  node_comments.created AS node_comments_created,
  node_comments.changed AS node_comments_changed,
  node_comments__node_comment_statistics.last_comment_timestamp AS node_comments__node_comment_statistics_last_comment_timestamp
FROM comments comments
LEFT JOIN node node_comments ON comments.nid = node_comments.nid
LEFT JOIN history history_user ON node_comments.nid = history_user.nid
  AND history_user.uid =10
LEFT JOIN comments node_comments__comments ON node_comments.nid = node_comments__comments.nid
INNER JOIN node_comment_statistics node_comments__node_comment_statistics ON node_comments.nid = node_comments__node_comment_statistics.nid
WHERE
  (
    node_comments.status <>0
    OR node_comments.uid =10
    OR 1 =1
  )
  AND
  (
    (
      history_user.timestamp IS NULL
      AND
      (
        node_comments.changed > ( 1242848360 -2592001 ) OR node_comments__node_comment_statistics.last_comment_timestamp > ( 1242848360 -2592001 )
      )
    )
    OR history_user.timestamp < node_comments.changed
    OR history_user.timestamp < node_comments__node_comment_statistics.last_comment_timestamp
  )
  ORDER BY comments_timestamp DESC
  LIMIT 0 , 6

this last query returns the information I need.

Here is my view

$view = new view;
$view->name = 'comments_recent';
$view->description = 'Contains a block and a page to list recent comments; the block will automatically link to the page, which displays the comment body as well as a link to the node.';
$view->tag = 'default';
$view->view_php = '';
$view->base_table = 'comments';
$view->is_cacheable = FALSE;
$view->api_version = 2;
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
$handler = $view->new_display('default', 'Defaults', 'default');
$handler->override_option('relationships', array(
  'nid' => array(
    'id' => 'nid',
    'table' => 'comments',
    'field' => 'nid',
    'label' => 'Node',
    'required' => FALSE,
  ),
));
$handler->override_option('fields', array(
  'subject' => array(
    'id' => 'subject',
    'table' => 'comments',
    'field' => 'subject',
    'label' => '',
    'link_to_comment' => 1,
    'relationship' => 'none',
  ),
  'timestamp' => array(
    'id' => 'timestamp',
    'table' => 'comments',
    'field' => 'timestamp',
    'label' => '',
    'date_format' => 'time ago',
    'custom_date_format' => '',
    'relationship' => 'none',
  ),
  'view_comment' => array(
    'label' => '',
    'alter' => array(
      'alter_text' => 0,
      'text' => '',
      'make_link' => 0,
      'path' => '',
      'alt' => '',
      'prefix' => '',
      'suffix' => '',
      'help' => '',
      'trim' => 0,
      'max_length' => '',
      'word_boundary' => 1,
      'ellipsis' => 1,
      'strip_tags' => 0,
      'html' => 0,
    ),
    'text' => 'Read',
    'exclude' => 0,
    'id' => 'view_comment',
    'table' => 'comments',
    'field' => 'view_comment',
    'override' => array(
      'button' => 'Override',
    ),
    'relationship' => 'nid',
  ),
  'timestamp_1' => array(
    'label' => '',
    'alter' => array(
      'alter_text' => 0,
      'text' => '',
      'make_link' => 0,
      'path' => '',
      'alt' => '',
      'prefix' => '',
      'suffix' => '',
      'help' => '',
      'trim' => 0,
      'max_length' => '',
      'word_boundary' => 1,
      'ellipsis' => 1,
      'strip_tags' => 0,
      'html' => 0,
    ),
    'link_to_node' => 0,
    'comments' => 1,
    'exclude' => 0,
    'id' => 'timestamp_1',
    'table' => 'history_user',
    'field' => 'timestamp',
    'relationship' => 'nid',
    'override' => array(
      'button' => 'Override',
    ),
  ),
));
$handler->override_option('sorts', array(
  'timestamp' => array(
    'id' => 'timestamp',
    'table' => 'comments',
    'field' => 'timestamp',
    'order' => 'DESC',
    'granularity' => 'second',
    'relationship' => 'none',
  ),
));
$handler->override_option('filters', array(
  'status_extra' => array(
    'id' => 'status_extra',
    'table' => 'node',
    'field' => 'status_extra',
    'operator' => '=',
    'value' => '',
    'group' => 0,
    'exposed' => FALSE,
    'expose' => array(
      'operator' => FALSE,
      'label' => '',
    ),
    'relationship' => 'nid',
  ),
  'timestamp' => array(
    'operator' => '=',
    'value' => '',
    'group' => '0',
    'exposed' => FALSE,
    'expose' => array(
      'operator' => FALSE,
      'label' => '',
    ),
    'id' => 'timestamp',
    'table' => 'history_user',
    'field' => 'timestamp',
    'relationship' => 'nid',
    'override' => array(
      'button' => 'Override',
    ),
  ),
));
$handler->override_option('access', array(
  'type' => 'none',
  'role' => array(),
  'perm' => '',
));
$handler->override_option('title', 'Recent comments');
$handler->override_option('items_per_page', 20);
$handler->override_option('use_more', 1);
$handler->override_option('style_plugin', 'table');
$handler->override_option('style_options', array(
  'grouping' => '',
  'override' => 1,
  'sticky' => 0,
  'order' => 'asc',
  'columns' => array(
    'subject' => 'subject',
    'timestamp' => 'timestamp',
    'view_comment' => 'view_comment',
    'timestamp_1' => 'timestamp_1',
  ),
  'info' => array(
    'subject' => array(
      'sortable' => 1,
      'separator' => '',
    ),
    'timestamp' => array(
      'sortable' => 1,
      'separator' => '',
    ),
    'view_comment' => array(
      'separator' => '',
    ),
    'timestamp_1' => array(
      'separator' => '',
    ),
  ),
  'default' => '-1',
));
$handler = $view->new_display('page', 'Page', 'page');
$handler->override_option('fields', array(
  'title' => array(
    'id' => 'title',
    'table' => 'node',
    'field' => 'title',
    'label' => 'Reply to',
    'relationship' => 'nid',
    'link_to_node' => 1,
  ),
  'timestamp' => array(
    'id' => 'timestamp',
    'table' => 'comments',
    'field' => 'timestamp',
    'label' => '',
    'date_format' => 'time ago',
    'custom_date_format' => '',
    'relationship' => 'none',
  ),
  'subject' => array(
    'id' => 'subject',
    'table' => 'comments',
    'field' => 'subject',
    'label' => '',
    'link_to_comment' => 1,
    'relationship' => 'none',
  ),
  'comment' => array(
    'id' => 'comment',
    'table' => 'comments',
    'field' => 'comment',
    'label' => '',
    'relationship' => 'none',
  ),
));
$handler->override_option('items_per_page', 25);
$handler->override_option('use_pager', '1');
$handler->override_option('style_plugin', 'list');
$handler->override_option('style_options', array(
  'type' => 'ul',
));
$handler->override_option('row_options', array(
  'inline' => array(
    'title' => 'title',
    'timestamp' => 'timestamp',
  ),
  'separator' => '&nbsp;',
));
$handler->override_option('path', 'comments/recent');
$handler->override_option('menu', array(
  'type' => 'none',
  'title' => '',
  'description' => '',
  'weight' => 0,
  'name' => 'navigation',
));
$handler->override_option('tab_options', array(
  'type' => 'none',
  'title' => '',
  'description' => '',
  'weight' => 0,
));
$handler = $view->new_display('block', 'Block', 'block');
$handler->override_option('filters', array(
  'status_extra' => array(
    'id' => 'status_extra',
    'table' => 'node',
    'field' => 'status_extra',
    'operator' => '=',
    'value' => '',
    'group' => 0,
    'exposed' => FALSE,
    'expose' => array(
      'operator' => FALSE,
      'label' => '',
    ),
    'relationship' => 'nid',
  ),
  'timestamp' => array(
    'operator' => '=',
    'value' => '',
    'group' => '0',
    'exposed' => FALSE,
    'expose' => array(
      'operator' => FALSE,
      'label' => '',
    ),
    'id' => 'timestamp',
    'table' => 'history_user',
    'field' => 'timestamp',
    'relationship' => 'nid',
    'override' => array(
      'button' => 'Use default',
    ),
  ),
));
$handler->override_option('items_per_page', 6);
$handler->override_option('block_description', 'Recent comments view');
$handler->override_option('block_caching', -1);
$handler = $view->new_display('feed', 'Feed', 'feed_1');
$handler->override_option('style_plugin', 'rss');
$handler->override_option('style_options', array(
  'mission_description' => FALSE,
  'description' => '',
));
$handler->override_option('row_plugin', 'comment_rss');
$handler->override_option('path', 'new_comments.rss');
$handler->override_option('menu', array(
  'type' => 'none',
  'title' => '',
  'description' => '',
  'weight' => 0,
  'name' => 'navigation',
));
$handler->override_option('tab_options', array(
  'type' => 'none',
  'title' => '',
  'description' => '',
  'weight' => 0,
));
$handler->override_option('displays', array());
$handler->override_option('sitename_title', FALSE);

CommentFileSizeAuthor
#4 468484-views-use_relationship.patch951 bytesdawehner

Comments

dawehner’s picture

I decided that people on my site don't need to see the comments in the list that they have already read so I created a filter that would filter on "(Node) Node: Has new content" and included new comments in the filter.

But you are trying to use a filter for nodes that the user didn't looked at yet, mh not sure how this can be made possible

but yes i can reproduce the problem!

jrglasgow’s picture

it is possible because ther is a table (user_history I think) that keeps track of which nodes/comments a user loaded and when. If the date the user looked at the node is < the node's/comment's last edit date it is marked "Updated", if there is no record of the user accessing the node/comment it is marked new

raintonr’s picture

Sort of related to this... if one creates a view to display comments, in the same way one cannot remove seen comments, one cannot flag those unseen with a 'new' tag or similar.

I don't really know much about views2, but seems that somewhere the join between user history and comments themselves needs making. Seems one will have to go through node to do that?

Out of interest, the closest I can find to this is to have a node view and show comments on nodes. Then use the 'is updated' node field and tell it to check comments too. Sadly this flags all comments on a node as unseen when a new one is added, which is hardly ideal. Also, the query views produces is terribly slow (over 1 second to execute on my site - 10,000 nodes, 35,000 comments):

SELECT node.nid AS nid,
   comments.subject AS comments_subject,
   comments.cid AS comments_cid,
   comments.nid AS comments_nid,
   comments.timestamp AS comments_timestamp,
   history_user.timestamp AS history_user_timestamp,
   history_user.nid AS history_user_nid,
   node.created AS node_created,
   node.changed AS node_changed,
   node_comment_statistics.last_comment_timestamp AS node_comment_statistics_last_comment_timestamp
 FROM node node 
 INNER JOIN node_comment_statistics node_comment_statistics ON node.nid = node_comment_statistics.nid
 LEFT JOIN comments comments ON node.nid = comments.nid
 LEFT JOIN history history_user ON node.nid = history_user.nid AND history_user.uid = ***CURRENT_USER***
 WHERE (node.status <> 0) AND (node_comment_statistics.comment_count > 0) AND (comments.status = 0)
   ORDER BY comments_timestamp DESC
dawehner’s picture

Version: 6.x-2.5 » 6.x-2.x-dev
Status: Active » Needs review
StatusFileSize
new951 bytes

Here is a patch for this issue. The relationship was missed for the handler.

srobert72’s picture

Same error

merlinofchaos’s picture

Status: Needs review » Fixed

The patch does indeed fix the error, so I committed it.

The query the view produces is brutally slow. Like, takes 20 seconds to run, slow. SO fixing the bug isn't really going to help. This one is just going to suck.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.