I am trying to display a CCK computed field with the views module. One catch is that the CCK computed field is NOT stored in the database. I need it calculated on the fly, that's why the data is NOT stored in the database?

The CCK computed field I created does NOT show up in the fields list on the Views form. Is that because the field is NOT stored in the database?

Comments

Moonshine’s picture

Yes, Views just generates SQL queries for retrieving saved data.

Assuming your value *truely needs to be recalculated every time, it would be quite a costly to do what your describing. You'd have to imagine that every node with this field would need to be loaded and have the calculation run before any limiting is done.

welzie’s picture

Thanks for the reply.

The computed field I need to display is an "overall rating" that is calculated by averaging multiple ratings. IMHO the ideal solution would be to calculate that on the fly.

So I can't use the "notafield" field setting to include this computed field into my view?

Moonshine’s picture

Howdy...

Well, I guess I don't really have a good picture yet of why the computed average couldn't be saved and updated as needed. (for full Views compatibility, performance, etc) It sounds like there is a reason you in mind though :) I guess my three questions would be:

- Are these ratings (that are averaged into the overall rating) all fields in some content type(s)?
- How and how often would these ratings change ?
- How many items (nodes) are we talking about?

As far as "notafield" items go, those field types can't be used to filter or sort results, if that's what you are after. The are ways to inject sql computations into Views queries programatically, if that's really what needs to be done. However IMO it's better not to head down that route unless you really know it's needed. There are also options in outputting/themeing the view if you are just interested in display of the a computed value like that, rather then sorting or filtering by it.

welzie’s picture

Thx again for the help. I truly appreciate the assistance/advice.

1)Yes the ratings fields are fivestar cck fields in a Rating node. Each Rating node contains multiple "sub ratings", for example "cost rating" and "customer service rating". The Rating node is associated to a Company node, using cck node reference field, which the rating is for. I am successful in getting the view to display a list of Company nodes with each "sub rating", but I can't figure out how to make it display the calculated overall rating.

2)The rating would change any time someone entered a rating. Probably between 5 and 20 times a day.

3)The total number of nodes (in my case Company nodes) for this view would be between 250 and 500.

I definitely need to sort by the overall rating. From you comments it seems that you are suggesting that the best thing is to store the overall rating the in database. The problem is I don't really know how to do that since the Rating node is separate from the Company node. I guess I would have to add special hook sql that would update the overall rating column in the Company node table each time a Rating node was inserted/updated.

welzie’s picture

I forgot to ask if there were any other drupal modules that you suggest for displaying list/paged/sortable content?

Thx again

Moonshine’s picture

If you need to sort by this overall computed average, then I'd definitely try to get it in the db. That will give you max flexibility and performance in the end.

The real key here is that you are using the Fivestar module which uses the Voting API to keep track of its results. That's a *good thing, as the Voting API has a bunch of hooks. :) It's been a while since I've played with it, but there should be some decent docs out there on the hooks it offers. For example, hook_voting_api_insert() or hook_votingapi_results() may be the right spot. Then your code can just take the new results, do the calculation and save that result into a appropriate node at the same time the vote is cast.

I think if you head that direction you'll be happy in the end.

Edit: oops... Looks like the results call is an alter hook like hook_votingapi_results_alter().. anyways, it's been a while for sure. You can find some information in the API.txt file that comes with the Voting API module or http://cvs.drupal.org/viewvc.py/drupal/contributions/modules/votingapi/A...

Moonshine’s picture

Really Views is *the* module for sorted, filter, pages, etc output. Outside of that, you'd be looking at just making the pager queries yourself and using Drupal's theme calls to output things. It looks like you're using Drupal 5.x, and views should be able to do quite a bit. However it's much more powerful in Drupal 6.x as it's been redesigned from the ground up.

vood002’s picture

I'm running into a pretty similar issue....

You might find http://drupal.org/node/195013 to be useful, it contains some code to do a batch update of all nodes of a certain type....this may or may not work for you, as you'd have to run it with a cron or something.

I intend to use this toward a slightly different purpose...when users are browsing nodes on my site with list view in views, I want to have a string of icons next to the node title alerting the user if the node is subscription only, requires special equipment, etc. Computed field is the best answer I've seen so far for this...if there is a better one I'd love to hear about it.

najibx’s picture

"computed field" compute from data CCk field and being display in Views.
is it possible currently to use value that users enter (provide a form) and exposed filter, then computed it againts certain stored CCK fields and display dynamically on Views?

colan’s picture

Status: Active » Closed (won't fix)

Version 5 is no longer supported.