Drupal 7: comment count error on node creation issue

Notice: Undefined property: stdClass::$comment_count in comment_node_page_additions() line 727 of comment.module

Hello, it happens when node_comment_statistics table is empty. I had this problem after adding content directly in node content : the trigger that is counting comments is disable.
You can verify if this table is empty or not, or a relation between nid in node table and nid in node_comment_statistics is not broken.
This query works fine :
INSERT INTO `node_comment_statistics` (`nid`, `cid`, `last_comment_timestamp`, `last_comment_name`, `last_comment_uid`, `comment_count`)
SELECT nid, 0, UNIX_TIMESTAMP(), NULL, 1, 0 FROM node

https://www.drupal.org/project/drupal/issues/1036668#comment-4534888

The solution

TRUNCATE TABLE node_comment_statistics;
INSERT INTO
    node_comment_statistics
(
    nid,
    last_comment_timestamp,
    last_comment_name,
    last_comment_uid,
    comment_count
)
SELECT
    n.nid,
    IFNULL(last_comment.created,n.changed) AS last_comment_timestamp,
    IFNULL(last_comment.name,null) AS last_comment_name,
    IFNULL(last_comment.uid,n.uid) AS last_comment_uid,
    IFNULL(comment_count.comment_count,0) AS comment_count
FROM
    node AS n
    LEFT OUTER JOIN (SELECT nid, COUNT(*) AS comment_count FROM comment WHERE status=1 GROUP BY nid) AS comment_count ON comment_count.nid=n.nid
    LEFT OUTER JOIN (SELECT nid, MAX(cid) AS max_cid FROM comment WHERE status=1 GROUP by nid) AS max_node_comment ON max_node_comment.nid=n.nid
    LEFT OUTER JOIN (SELECT cid,uid,name,created FROM comment ORDER BY cid DESC LIMIT 1) AS last_comment ON last_comment.cid=max_node_comment.max_cid
WHERE
    n.status=1
ORDER BY
    n.nid; 

Credit: http://drupal.org/node/137458

Leave a Reply