Currently, the 'File Usage: Content' relationship produces the following query:
SELECT file_managed.filename AS file_managed_filename, file_managed.fid AS fid, file_managed.filesize AS file_managed_filesize, file_managed.uri AS file_managed_uri, node_file_usage.nid AS node_file_usage_nid, node_file_usage.title AS node_file_usage_title
FROM
{file_managed} file_managed
LEFT JOIN {file_usage} file_usage ON file_managed.fid = file_usage.fid
LEFT JOIN {node} node_file_usage ON file_usage.id = node_file_usage.nid AND node_file_usage.type = 'node'
WHERE (( (node_file_usage.nid IS NOT NULL ) ))
LIMIT 10 OFFSET 0
which is returning 0 items due to the AND node_file_usage.type = 'node' piece.
The 'File Usage: User' relationship works correctly, so extrapolating that relationship definition leads to the fix. patch attached.
Comments
Comment #1
WorldFallz commentedthe fix produces the following corrected query:
Comment #2
WorldFallz commentedactually, no. That then breaks my download_count module views. Rather this patch seems to work in all cases.
Comment #3
TahitiPetey commentedWorldFallz, your patch works. However it removes the check for file_usage.type = 'node' on the LEFT JOIN (at least for me). Not sure if this will cause any problems down the road (it had occurred to me that the check seems rather redundant and unnecessary in the first place, but I don't know Views well enough to say...).
Comment #4
thinkyhead commentedI see in the latest 7.x-3.x dev that the offending 'extra' entry has simply been removed. This appears to be an error. As near as I can tell it should just duplicate the 'file_to_user' version of the 'extra' key with 'node' instead of 'user'. I've just done this on my current client's site and it produces the correct SQL. My patch for the current 7.x-3.x branch is attached.
Comment #5
WorldFallz commentedthe current views 7.x-3.x-dev remains as originally reported (with
'extra' => array(array('field' => 'type', 'value' => 'node')),.And your patch is identical to the one I originally provided-- the question remains as described in #3. Would be nice to get some more eyes on this.
Comment #6
thinkyhead commentedIndeed, my patch makes the same correction as your original patch. My question is, if the file_to_node join is incorrect, wouldn't the file_to_user join also be wrong?
So, to follow up on the issue, what does the broken query look like for your download_count view, as you mentioned in #2? Maybe there's a more subtle solution for that.
Comment #7
WorldFallz commentedThanks for following up thinkyhead.
The query I get with the patch original patch is:
Which also results in an error:
And my relationship definition is:
I add that relationship, and then chain the 'file usage: content' relationship on top of that to get at the node title.
I've been banging my head against the wall trying to get this resolved for weeks, and no matter what I do it seems the only thing that works is #2.
Comment #8
thinkyhead commentedWell you can see the
file_usagetable is being JOINed with a name reassignment tofile_managed_download_count__file_usage- and it's not thatfile_usage.typeis itself problematic. Perhaps there's a more fundamental issue in the fact that Views isn't keeping track of the name reassignment.See if there's some way to make that last JOIN look like this:
LEFT JOIN {node} node_file_usage ON file_managed_download_count__file_usage.id = node_file_usage.nid AND file_managed_download_count__file_usage.type = 'node'Comment #9
grossmann commentedI had similar problems and read through this issue. The patch on #0 (issue summary) worked for me but I got confused with the other patches.
@thinkyhead: Your patch works on another relationship than the original one (see line numbers of you patch).
This is on the ['node_to_file'] relationship not the ['file_to_node'] relationship which is targeted in #0.
I did not test if this fixes node_to_file relationship but the on in #0 works. Maybe this is why you think they removed it on the latest dev (see #7).
grossmann-mcs
Comment #10
Johnny vd Laar commentedThe bug should be fixed in a higher level I think. Attached is some code that tries to resolve the alias for the table instead of just dropping it.
Comment #11
WorldFallz commentedI still get blank node titles for the patch in #10 against a fresh checkout of d7 dev. No error though.
Attached is a simple views export for the view I use to test this.
Comment #12
alexjarvis commentedApplying the change in #4 to a Views 3.1 install's $data['file_usage']['file_to_node'] fix the problem for me.
Comment #13
bschilt commentedPatch in #0 worked for me as I was doing a file to node relationship.
Comment #14
Johnny vd Laar commentedThe view I was testing with was a lot more complex. I had a view of nodes that had media items and the files had a taxonomy field and the taxonomy field had a link field which I wanted to see in the nodes. So perhaps my change only worked well in my particular edge case.
Comment #15
andrewbelcher commentedJust had a look at the repository log... Looks like in commit #289b6c97b722596f7bdd505e1e8bd15f3be32b07 the
'table' => 'file_usage'was removed (along with'operator' => '='). It looks like there are a few different problems here:1) The removal of the 'table' broke the relationship as it tried to join based on the wrong column. This is fixed by adding back in (as done in the original patch).
2) Simply putting it back in brings up a different issue where the table name rather than the alias is getting used when the relationship get's turned into SQL. This looks like it's fixed by the patch in comment #10... This is the issue that was breaking the download count and possibly other potential things.
3) Simply removing the check doesn't really solve the issue, it just creates a different one if you have files used by multiple entities which have the same id.
rlmumford is currently applying the two patches, testing and hopefully soon will post a patch which solves both issues!
Comment #16
rlmumfordSo here's the patch
Comment #17
WorldFallz commentedawesome-- thanks so much for taking time to explain the details andrew. I feel like I'm stabbing in the dark when I try to trouble shoot views issues like this and that type of explanation is really instructive.
And thanks for rolling the patch rlmumford!
I'm testing it now, and it seems to be fixing the issues.
I still have one odd issue, though fixable with a filter, that doesn't make sense to me.
I get the following query:
However, the result set is still including files attached to user entities in spite of the
LEFT JOIN {node} node_file_usage ON file_managed_download_count__file_usage.id = node_file_usage.nid AND file_managed_download_count__file_usage.type = 'node'.That's probably specific to the download_count module, though so I'm not sure it should hold up this patch. Hopefully some others will test it out and we can rtbc this puppy.
Comment #18
WorldFallz commentedAdding a
(File) File Usage: Entity type (= node)filter produces the following query:Which correctly returns only nodes. Still don't know why though.
Comment #19
rlmumfordHave you tried setting the required flag on the relationship?
Comment #20
WorldFallz commentedrlmumford ftw-- I didn't realize that flag was tied to switching to an inner join. brilliant, lol. testing with some more complex views, but so far this seems to be the fix.
Comment #21
WorldFallz commentedi've test this quite a bit with all sorts of complex views, and it seems to be the fix. It would be good if someone else on this thread also tested it so we can rtbc it.
Comment #22
Johnny vd Laar commentedthe patch also works for me
Comment #23
dawehnerThis line would probably need some documentation, so readers can understand what this means.
Sure i know what this means, and it makes somehow sense, it would be cool to add a one line description + some more stuff above in the views_join documentation.
Comment #24
rlmumfordHere's the patch re-rolled with some (hopefully) helpful comments
Comment #25
rlmumfordFixed a typo
Comment #26
rlmumfordComment #27
dawehnerHere is a rerole of the patch with a short docu improvement.
Comment #28
tim.plunkettTriggering the testbot.
Comment #30
rlmumfordRerolled against latest dev
Comment #31
WorldFallz commentedbot tests pass, patch applies cleanly, retested again with some complex views and everything is working. New comments look good also.
looks gtg.
Comment #32
btmash commentedFunny, I was about to post the same patch (minus the commenting) as I ran into this issue. So its a +1 for me as well.
Comment #33
tim.plunkettLeaving as RTBC, but I cleaned up the comments a little bit.
Comment #34
dawehnerHere is a slightly easier patch with less possible fails.
Comment #35
WorldFallz commentedviews still work and definitely easier read. looks good.
Comment #36
dawehnerThanks for all contributors.
Committed to 7.x-3.x
Comment #37
WorldFallz commentedawesome... thanks everyone!
Comment #38
chris matthews commentedThe Drupal 6 branch is no longer supported, please check with the D6LTS project if you need further support. For more information as to why this issue was closed, please see issue #3030347: Plan to clean process issue queue