I was getting the error message:

notice: Undefined property: stdClass::$comment_count in modules/comments/comments.module on line 724.

I simply solved it by replacing line 724 by:

if ((!empty($node->comment_count) && user_access('access comments')) || user_access('administer comments')) {

This looks like something that was forgotten since:

(!empty($node->comment_count) && user_access('access comments'))

is the way this is checked everywhere else in the module.

[EDIT]

solution

Read this issue to solve this problem: http://drupal.org/node/137458#comment-5072066

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

droplet’s picture

so how to reproduce this error, thanks

parraccourci’s picture

I have actually no idea and have no time to spend on that.

I'm not sure how the comment module works but it looks like $node->comment_count might not be set in some case.

What i propose here is to replace "!$node->comment_count" by "!empty($node->comment_count)" which has the same effect (see below) and makes this part of the code more robust. plus the way i propose is already used in other parts of the same file, it can't be wrong.

$a = new stdClass();
$a->a = 1;
$a->b = 0;
var_dump(empty($a->a)); // 1 => False
var_dump(empty($a->b)); // 0 => True
var_dump(empty($a->c)); // Undefined => True and no PHP notice
var_dump((bool) $a->c); // Undefined => returns FALSE (WHICH ISN'T WHAT SHOULD HAPPEN) and generates PHP notice
Azol’s picture

Version: 7.0 » 7.x-dev
Status: Active » Needs review
FileSize
1.03 KB

I experience the same error.
Here is the patch with solution proposed in #2.

scottrouse’s picture

Status: Needs review » Reviewed & tested by the community

I reported this error at #1036668: Undefined property: stdClass::$comment_count in comment_node_page_additions() when I couldn't find this issue.

I've tested the patch in #3, and it resolves the issue for me.

Thanks.

droplet’s picture

Status: Reviewed & tested by the community » Needs work

okay. so it's a popular error ? anyone can share how to reproduce this error to other drupaler.

#2, #3 always works.. because it checks the error at error line. But the comment_count should always there, we need to figure out why missing. otherwise we will add empty checks everywhere in drupal.

scottrouse’s picture

For me, it's been hard to specify exactly what began to trigger the error. I can say that, without the patch in #3, I see the error on each individual node page (node/[nid]), but not on any overview pages (front, taxonomy/term/[tid], Views pages, etc). I see it on node pages regardless of whether comments are open, closed or hidden.

If there is more about my particular installation I can provide which may be helpful, please let me know, and I will be happy to do so.

Thanks.

Azol’s picture

The code in question belongs to function comment_node_page_additions($node), but let us take a look at the function comment_node_view($node, $view_mode):

line 627:
if (!empty($node->comment_count)) {
which looks totally reasonable to me;

line 675:
if ($comment_form_location == COMMENT_FORM_SEPARATE_PAGE || (!empty($node->comment_count) && user_access('access comments'))) {
same;

lines 706-708:

if ($node->comment && $view_mode == 'full' && node_is_page($node) && empty($node->in_preview)) {
      $node->content['comments'] = comment_node_page_additions($node);
    }

which call our "questionable" comment_node_page_additions, but the reason is: this function is called no matter if $node->comment_count is empty or not. So, similar to previous IF statement in line 627, we cannot safely use $node->comment_count condition.
Either the logic of function comment_node_view is broken, or the code in question is merely an overlook.

I wasn't able to reproduce this error with mint D7 install, still trying to figure it out.

Also, I am looking at this code (starting with line 1241 of comment.module):

function comment_node_load($nodes, $types) {
  $comments_enabled = array();

  // Check if comments are enabled for each node. If comments are disabled,
  // assign values without hitting the database.
  foreach ($nodes as $node) {
    // Store whether comments are enabled for this node.
    if ($node->comment != COMMENT_NODE_HIDDEN) {
      $comments_enabled[] = $node->nid;
    }
    else {
      $node->cid = 0;
      $node->last_comment_timestamp = $node->created;
      $node->last_comment_name = '';
      $node->last_comment_uid = $node->uid;
      $node->comment_count = 0;
    }
  }

  // For nodes with comments enabled, fetch information from the database.
  if (!empty($comments_enabled)) {
    $result = db_query('SELECT nid, cid, last_comment_timestamp, last_comment_name, last_comment_uid, comment_count FROM {node_comment_statistics} WHERE nid IN (:comments_enabled)', array(':comments_enabled' => $comments_enabled));
    foreach ($result as $record) {
      $nodes[$record->nid]->cid = $record->cid;
      $nodes[$record->nid]->last_comment_timestamp = $record->last_comment_timestamp;
      $nodes[$record->nid]->last_comment_name = $record->last_comment_name;
      $nodes[$record->nid]->last_comment_uid = $record->last_comment_uid;
      $nodes[$record->nid]->comment_count = $record->comment_count;
    }
  }
}

By default, comment_count column of node_comment_statistics table should contain 0 (unless there are comments posted), which IS actually the case on affected site, so all clear here (I believe). Or not?

geerlingguy’s picture

Just had this problem crop up on one of my sites, after trying to delete a node (but when deleting the node, the redirect module threw an error, so the node didn't actually delete). Now it shows up on every page load.

Here's the error I got when trying to delete the node:

Recoverable fatal error: Argument 1 passed to redirect_path_delete() must be an array, boolean given in redirect_path_delete() (line 277 of /home/~/domains/d7multisite/sites/all/modules/redirect/redirect.module).

Obviously, the patch above fixes the problem for me, but doesn't solve the overriding issue. I think it may have been the fact that my node wasn't deleted thorougly/properly due to the redirect error.

[Edit: Doing some more checking (this error only started cropping up after I deleted that node)... will post results below:]

  • The node table seems to be perfectly intact, so node db table not causing the error.
  • The comment table seems to be intact as well (no data yet for this site).
  • The node_comment_statistics table did have a difference - there were no rows for the two nodes I tried deleting. When I added rows back in for those two nodes, manually, the error vanished.

Sounds like what might be happening is that if node_delete() (or whatever it is in D7) errors out mid-deletion, this error can crop up, because the node's entry in the node_comment_statistics table is deleted, but the actual node is not deleted.

j0rd’s picture

I also can not delete my nodes I've created programatically under Drupal 7.

They've been created with no matching entry in `node_comment_statistics` which I assume is the problem. Here's my code for creating my nodes:


  $event = new stdClass(); 
  $event->type = 'mynode'; 
  node_object_prepare($event);
                                                                                       
  dpm(array('mynode' => $event));
  $event->language = 'en';
  $event->title = 'here\'s the title';
  $event->body[$event->language][0]['value'] = 'boDY';
  $event->body[$event->language][0]['summary'] = 'teaser';
  $event->body[$event->language][0]['format'] = 'filtered_html';
                                                                                                                     
  $event->field_date[$event->language][0]['value'] = '2011-02-23T12:30:00'; 
  $event->field_date[$event->language][0]['value2'] = '2011-02-23T12:30:00'; 
                                                                                                                                                                                                        
  $event->field_location[$event->language][0]['nid'] = $winner->nid; // nid of suggestion 
                                  
  node_save($event);                                                                                                 

I get this warning when viewing my node

Notice: Undefined property: stdClass::$comment_count in comment_node_page_additions() (line 724 of /home/www/beercampbangkok.org/htdocs/modules/comment/comment.module).

Unless I'm doing something wrong here when creating my nodes, I believe this is a bug and should be resolved. I would assume though that using `node_object_prepare()` would initialize things properly, so that I can save my node with out error.

j0rd’s picture

Additionally, I noticed that upon my first attempt to delete a node which had an entry in the `node_comment_statistics` table, the entry in `node_comment_statistic` was deleted, but the node was not. Thus causing this error to happen.

j0rd’s picture

Ok, so I did a little more digging.

Turns out I had a bad module (my own!), which was doing a var_dump() and exit() on hook_node_delete(). I forgot it was there.

This was causing the node to ever get deleted, and then caused this error message:

Notice: Undefined property: stdClass::$comment_count in comment_node_page_additions() (line 724 of /home/www/beercampbangkok.org/htdocs/modules/comment/comment.module).

This comes down to the node.module's `node_delete_multiple($nids)` function.

The try catch in there will could call comment_node_delete and then another one later down the chain would toss an exception, thus removing the entry from the `node_comment_statistics` table.

In the exception handling we're calling `$transaction->rollback();`, but I'm not familiar enough with D7 yet to understand if that will re-insert the entry into `node_comment_statistics`. If not, we'll be left in an un-desirable state.

nortont’s picture

I too am getting this error.
For me it occured as soon as I enabled the Comments module. If I disable the module, the error goes away.

I am getting additional errors when I enable the module that may be related and I have not found an answer to yet

Error
The website encountered an unexpected error. Please try again later.
PDOException: SQLSTATE[HY000]: General error: 1364 Field 'last_updated' doesn't have a default value: INSERT INTO {node_comment_statistics} (last_comment_timestamp, last_comment_uid, nid, comment_count, last_comment_name) SELECT n.created AS last_comment_timestamp, n.uid AS last_comment_uid, n.nid AS nid, 0 AS comment_count, NULL AS last_comment_name FROM {node} n LEFT OUTER JOIN {node_comment_statistics} ncs ON ncs.nid = n.nid WHERE (ncs.comment_count IS NULL ) ; Array ( ) in comment_enable() (line 48 of /home/.../public_html/drupal/modules/comment/comment.install).

Any suggestions?

pillarsdotnet’s picture

Yeah. Alter your database so that field 'last_updated' has a default value.

nortont’s picture

Replacing line 724 with the following worked as per point 1

if ((!empty($node->comment_count) && user_access('access comments')) || user_access('administer comments')) {

The patch did not work

sirkitree’s picture

The change suggested in #15 worked for me.

pillarsdotnet’s picture

This is getting rediculous.

The originally reported problem occurred because the original poster had a bad module with invalid code. Once he corrected his module, the error vanished. See his report in #12, at which point he should have marked this issue "Fixed."

The error reported in #13 has nothing whatsoever to do with this issue. It is happening because the node_comment_statistics table has an outdated schema, with a "last_updated" field that is no longer supported by the current code. The fix is to either manually correct the database or completely uninstall and reinstall the comments module. (Note that to "uninstall" you must first "disable", then click on the "Uninstall" tab of the modules page.)

The change in #15 is identical to the patch in #3, which turned out to be unnecessary, since it partially obscured the real problem, discovered in #12.

I'm gonna mark this problem "Won't fix; works as designed" because I'm absolutely not convinced there is an error in the current development version of the code.

Azol’s picture

Status: Needs work » Closed (works as designed)

...which in no way explains the inconsistent use of $node->comment_count throughout the code, described in #7.

pillarsdotnet’s picture

@Azol -- If you want to generally clean up the code for consistency, then please submit a D8 patch. After it is accepted, feel free to submit a backport patch to D7.

But please do so in a separate issue. This issue is about a possible bug in the existing code that may have triggered a PHP notice. It turns out that the notice was triggered by a bug in another module, not by a bug in Drupal core. Indeed. your suggested change to Drupal core, though otherwise salutary, may make it harder to discover unrelated bugs in other modules, such as the one found in #12.

Meanwhile Drupal 7.x is now "stable", which means that patches are very unlikely to be accepted unless:

  • They fix a known, repeatable bug without changing the API or UI

-- or --

  • They have already been accepted in the "development" (now 8.x) version and are now being back-ported for consistency.
bryancasler’s picture

Was another issue created in response to #19 that I can follow?

Azol’s picture

None that I am aware of.
None of my sites now have this problem and there is no known way to reproduce it.

bryancasler’s picture

I think this conversation was continued over here. #1171568: Notice: Undefined property: stdClass::$comment_count in comment_node_page_additions() (line 721 I put in my two cents about how I ended up getting the error.

sammyframson’s picture

Title: notice: Undefined property: stdClass::$comment_count in modules/comments/comments.module on line 724. » #15 Worked for me as well..

Seems to be resolved with the solution in the original post / reply #15...Thanks.

sammyframson’s picture

Title: #15 Worked for me as well.. » Notice: Undefined property: stdClass::$comment_count in comment_node_page_additions() (line 724 of /...
remaxos’s picture

I had same problem
"Notice: Undefined property: stdClass::$comment_count in comment_node_page_additions() (line 724 of /home/lamedice/public_html/modules/comment/comment.module)"

I solved so like in post #15...

i changed the line 724 whith this:if ((!empty($node->comment_count) && user_access('access comments')) || user_access('administer comments')) {

and worked !

Itangalo’s picture

Itangalo’s picture

Re-roll of the patch in #3, in Git style.
Cred to parraccourci and Azol.

sctech’s picture

I had this issue with D7 after migrating tables from a staging server. My issue was based on the fact that I was going from one content name to another and the comment fields were set differently. The new content name was more appropriate for our application.

Server #1 - Source
Content_type_1 had comments set read only
Tables node and node_revisions were showing field "comments" = 1
//Whether comments are allowed on this node (at the time of this revision): 0 = no, 1 = closed (read only), 2 = open (read/write).

Server#2 - Target
My new Content_type_2 had comments set off
Tables node and node_revisions showing field "comments" = 0

In phpmyadmin I ran two queries on Server#2.
I went to table node and ran UPDATE node SET comment = 0
Then I went to table node_revisions and ran UPDATE node_revisions SET comment = 0

Hope this helps someone save an hour.

TimelessDomain’s picture

#27 patch fixed error for me

erikhopp’s picture

#27 patch also fixed this issue for me.

dmon000’s picture

#27 fixed problem for me also. Thanks!!

killua99’s picture

#27 patch fixed error for me

Azol’s picture

I do not doubt it fixes it for you (well, it kinda supposed to :), but it worth mentioning that this issue was prompty closed and it won't be fixed this way unless someone can figure out the way of reproducing the issue.

Check the http://drupal.org/node/137458#comment-5072066 for possible solution fixing this problem without hacking the core. It bears D5 tag, but seems like problem still stands and can sometimes appear while migrating from one major version to the next one or as a result of DB engine misbehaving.

czfed’s picture

#27 fix my problem, thx!!

Courtney.B’s picture

I received this error after I added a comment form and comments section to a Node Template. Using Views, Page Manager, and Panels.

Followed #17's advice and problem is fixed.

marcen0’s picture

Version: 7.x-dev » 7.9

Hi!
I have had the same problem when I was creating comment content. non-administrative users would comment and get the error instead of their comment, but the comment had been stored in the db. As i see patch #27 does not solve the issue because I did some debugging using

  if ((isset($node->comment_count) && $node->comment_count && user_access('access comments')) || user_access('administer comments')) 

which took away the error, but the non-administrative user could not view the comment and nothing was being installed in "node_comment_statistics" table. So i think the problem reside in whichever php class or function is responsible for connecting "node" and "node_comment_statistics" tables.

amirtaiar’s picture

Thank you,
That solve my problem.
I got the error after allowing user permission to manage comments.

jeffheaton’s picture

#33 worked for me too. No core hacking needed!

TechNikh’s picture

Dave Reid’s picture

manoloka’s picture

I can confirm that 33# fixed my issue

Thanks

manoloka’s picture

Issue summary: View changes

Added solution

func0der’s picture

Issue summary: View changes

All who are using #33, please be aware of this: https://www.drupal.org/node/1036668#comment-8955831

drupix’s picture

Maybe the correct solution ???

I had the same problem with a custom module who create custom content. Until now, I only used the UI to create custom content and I never had this problem.

After changing the weight of my module to "1" (higer than comment.module) the problem disapear for new custom content created.

To resolve the problem for custom nodes created before I changed the weight of my modules, I had to use the script #8.

Now, everything works fine.

func0der’s picture

Since there is no proper way to set the weight of a module from installation on, I would say that A solution, but not THE solution for the cause of the problem.

blogers’s picture

this work for me #8 https://www.drupal.org/node/1036668#comment-6085070

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; 
MahmoodZidan’s picture

#17 thanks for your comment.
It was a bad idea to play with the core. Your solution worked like charm. I was afraid I'll lose all the comment settings when I uninstall but it actually didn't.

keramsey’s picture

I am getting the same error with Drupal 7.64 on only one content page (on 1 domain access page, but not the rest of the domains the page is on). I do not have a 'last_updated' field in my node_comment_statistics table as described in #17. I also ran SQL script in #45 even though the table was not truncated. The nid of the content that had the error was not found in the node_comment_statistics table.

I manually applied the changes in #1 which removed the error. I am concerned about patching core in this way. Does anyone have any idea how I can search for a possible offending module as described in #17?

Could this be a Domain Access issue?