While the nodereference count fields will be updated when the node is saved or when the referencing node is saved, that isn't much help for pre-existing nodereferences to nodes created prior to the addition of a nodereference count field. It is probably best handled by a batch process that would update counts for all nodereference count fields. I think it makes the most sense to put this in a companion module in this project as it will generally only be useful to run once, immediately after adding nodereference count field(s) to an existing site.

I can build this as time allows. Patches are also always welcome.

Comments

geerlingguy’s picture

Subscribe

Flying Drupalist’s picture

subscribe

gmak’s picture

You could use VBO to update all of the nodes. Create a bulk view which lists all the nodes of the type you wish to update, add an operation (Publish) to apply. Run the view, click the 'select all' option and then apply the 'Publish' operation. This should re-save all of the nodes selected.

gilgabar’s picture

@gmak, that sounds like a good idea when the number of nodes to update is relatively small. I'm concerned that a site with a large number of exisiting nodereferences would timeout if you did something like that. Using the batch api should allow any arbitrary number of node counts to be updated without worry of timeouts. Unless VBO already implements the batch api or does something similar, do you know if that is the case?

Flying Drupalist’s picture

It definitely does use batch api, so you're alright there.

Actually if just resaving the content via VBO is a valid solution then this is as well as fixed for me. :)

gilgabar’s picture

Component: Code » Documentation

Ok, sounds like VBO is a good solution to this issue then. Thanks gmak and flying drupalist for your help. I've changed this to a documentation issue and I'll mark it fixed once I've updated the readme to include instructions for using VBO to update node counts.

geerlingguy’s picture

Using VBO, I'm getting the following error on one of my update operations (repeated for each row):

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 '= 248)AS nr_count' at line 1 query: SELECT (SELECT COUNT(*) FROM content_type_individual_registration WHERE field_event_reference_individual_nid = 248) + (SELECT COUNT(*) FROM content_ WHERE = 248)AS nr_count in /home/stlyouth/domains/registration/sites/all/modules/nodereference_count/nodereference_count.module on line 137.

The count updates worked for one content type, but not the second.

Running this update on another content type gives a similar 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 '= 13)AS nr_count' at line 1 query: SELECT (SELECT COUNT(*) FROM content_type_registration_group WHERE field_event_reference_nid = 13) + (SELECT COUNT(*) FROM content_ WHERE = 13)AS nr_count in /home/stlyouth/domains/registration/sites/all/modules/nodereference_count/nodereference_count.module on line 137.
gilgabar’s picture

It looks like the culprit is most likely a missing space. Notice the )AS. It should be ) AS. To confirm go to line 134 of nodereference_count.module and add a space in front of the AS. I think that should take care of it.

geerlingguy’s picture

Still getting this warning:

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 '= 13) AS nr_count' at line 1 query: SELECT (SELECT COUNT(*) FROM content_type_registration_group WHERE field_event_reference_nid = 13) + (SELECT COUNT(*) FROM content_ WHERE = 13) AS nr_count in /home/stlyouth/domains/registration/sites/all/modules/nodereference_count/nodereference_count.module on line 137.

Could it be this line?
SELECT COUNT(*) FROM content_ WHERE = 13

The first select statement works fine... but the second one doesn't seem to do anything.

gilgabar’s picture

Ok, I see. It isn't selecting the right table. I'll have to look into that. If you have the devel module installed you can get some more info about what is going on by sticking a dsm() call in the foreach loop that is generating that SELECT. Probably dsm($db_info); would be most helpful. That variable is populated by a call to CCK, so for some reason it isn't getting the correct data.

geerlingguy’s picture

I'm getting this on the results that turn out correctly:
table (String, 36 characters ) content_type_individual_registration

And this on the results that turn out incorrect:
table (String, 8 characters ) content_

I put in dsm($db_info); on line 112...

Could it possibly have to do with the fact that these group nodes are not only referencing another node type, but are also then referenced by a separate node type?

If I have more time later, I'll try to debug a little more.

gilgabar’s picture

Maybe it would be helpful if you map out the basics of the content types and the relationships you are using. It may be that I've made assumptions that aren't always true beyond my own basic requirements.

From the query it looks like you are trying to get an aggregate count from two separate nodereference fields. When you set up the nodereference count field what were the fields that you checked to have counted? It looks like one was 'field_event_reference'. What was the other?

Also does the error only occur with VBO or do you also see it when you save a node that is counted (or referencing)?

geerlingguy’s picture

I'm not trying to sum the counts of two separate nodereferences - rather I'm trying to have a count of all the "individual_registration" nodes that refer to an "event," and then separately (but in the same "event" content type) count all the "group" nodes that refer to the "event."

This error pops up both when I save an individual registration node from the Edit page and when I do a VBO on one node (or many).

Please see the three attached PNG files for reference - those are the three fields' Global Settings (the two event- fields are in the Event content type, and the group- field is in the Group content type.

gilgabar’s picture

Thanks. That's super helpful. I'll see if I can replicate the issue.

gilgabar’s picture

Ok, I think I have a fix. It looks like it's just a stupid oversight on my part. I was pretty sure I had tested that case, but apparently not. I'll have it committed shortly, but if you want to test immediately, just nest the contents of the foreach loop at line 110 inside an "if ($referencing_field) {}". Basically the field array returns all the fields available, but the ones that should not be counted have a value of zero. It was including those and passing zero to the content_fields() function which returned some garbage and generated errors. It should just skip those now.

gilgabar’s picture

The latest dev release has the fix now.

geerlingguy’s picture

I'll update and let you know how it goes - thanks for your work on this!

geerlingguy’s picture

Works swimmingly! I simply set up a views bulk operation, checked all the nodes to update, then did a 'publish node' operation on all of them...

A warning, though - it could take quite a while - I would use the Batch API feature of bulk operations, as you'll probably get a timeout if you have more than a few hundred nodes to update.

gilgabar’s picture

Status: Active » Needs review

Here is a proposed addition to the README with some VBO info. It could use some double checking by someone else to make sure I'm not forgetting something or missing something important by using a really simple test case.

Updating counts for pre-existing nodereferences
-----------------------------------------------
While the nodereference count fields will be updated when the node is saved or when the referencing node is saved, that isn't much help for pre-existing nodereferences to nodes created prior to the addition of a nodereference count field.  The Views Bulk Operations (VBO) module, http://drupal.org/project/views_bulk_operations, is the recommended solution .  Using VBO you can re-save all of the nodes that have a nodereference_count field.  That will update the count for each of those nodes.  The specifics will vary for each site, but the general instructions for doing this with VBO are as follows:

  - Install the Views and VBO modules if you do not already have them installed.
  - Go to /admin/build/views/add and create a new node view.
  - Create a new page display.
  - Change the style from Unformatted to Bulk Operations.
  - Select 'Use Batch API' under TO EXECUTE OPERATIONS.
  - Check 'Save post (node_save_action)' under SELECTED OPERATIONS.
  - Click 'Update default display'.
  - Add a path under Page Settings.
  - Add 'Node: Title' under Fields.
  - Add 'Node: Type' under Filters.  Be sure to select the node types that contain nodereference_count fields.
  - Save the view.
  - Visit the path that you set for the view.
  - Select all the nodes.
  - Click 'Save post'.  
  - Your nodereference_count fields should now be updated.
  - You are unlikely to need to use the view again, so feel free to delete it if you like.
geerlingguy’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me!

gilgabar’s picture

Status: Reviewed & tested by the community » Fixed

Great. The change is committed. I think we can consider this issue fixed. If there aren't any other problems found in the next week or two I think it should be safe to make a 1.0 release.

Status: Fixed » Closed (fixed)

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