views:

650

answers:

2

I am having a problem with views. I have a view and am passing it a taxonomy term by name to it. I then have a relationship to a related node. For my output fields I am returning the related Title and related Body. I however have duplicates in my results. I have turned distinct to yes but believe this is working on the nodes being return and not the related node. Any ideas how I can remove the duplicates?

Update

Below is the query being run when I only get the title

SELECT DISTINCT(node.nid) AS nid,
    node_node_data_field_wine_company.title AS node_node_data_field_wine_company_title,
    node_node_data_field_wine_company.nid AS node_node_data_field_wine_company_nid 
FROM node node  
LEFT JOIN content_type_wine node_data_field_wine_company ON node.vid = node_data_field_wine_company.vid 
INNER JOIN node node_node_data_field_wine_company ON node_data_field_wine_company.field_wine_company_nid = node_node_data_field_wine_company.nid  
LEFT JOIN term_node term_node ON node.vid = term_node.vid INNER JOIN term_data term_data ON term_node.tid = term_data.tid 
WHERE term_data.name = 'test'
GROUP BY nid

It looks like I should be grouping by node_node_data_field_wine_company_nid or selecting distinct values from there. Any ideas?

Update

It may not be possible using Normal vies. Below is my set up.

I have a taxonomy called Region. A region is applied to a custom content type called Wine. The Wine content type have a node reference field to a Company node type. Company is a custom node type.

I have a view listing all my regions. Clicking on the region I will pass that as an argument to a view (term name). From this region I want to return all the companies in that region.

To get this I need to get all the Wine items which have that region. With all the wine items with the region I need to get the unique company node reference. I will then return this.

One way round it would to be to give each company a region(s) to make the list. However I would rather it was worked out automatically from the wine type.

Any ideas?

A: 

Your analysis seems correct in that the distinct applies to the 'original' nodes, not the related ones. So you could try to 'reverse' your view construction, starting from the now 'related' nodes, adding a relation to the now 'original' nodes and filtering the results on the terms of those. The last point is the one I'm not sure about, in that I don't know if it is possible to apply the term filter on the nodes pulled in via a relationship, but it might be worth a try.

If it should turn out to be impossible to get your desired results by means of the 'standard' views functionality, there are various options to manipulate a view from custom code, but this would need a more detailed knowledge of the usage scenario (e.g. does it need to work with a pager and similar). If it looks like you need to go that route, you could enhance your question with a description of what you need to achieve exactly.


Edit: As for options to manipulate a view programmatically, you could take a look at the views module hooks. For small adjustments of the query result, one can implement hook_views_pre_render() and manipulate the returned resultset in $view->result directly (that is, after the query got executed).

However, for bigger manipulations (like in your case), one might implement hook_views_query_alter() and adjust the actual query before it is used to return a resultset. Care must be taken to only alter sort or filter criterias, but keeping the general structure of the returned data (e.g. it still needs to contain all the fields the view expects). While this approach gives huge flexibility, one needs to be aware that it is fragile concerning later changes applied to the view - if the view definition is changed in a way that alters the constructed query, the alteration done on hook_views_query_alter() might not work anymore, or cause weird results.

The views hooks get fired for every view, so one needs to check for the right view (and eventually display also) before applying any changes in there.

Henrik Opel
I don't think reversing it will work there is no connection back the other way. Will update the question
Linda