I am trying to load some conference materials, and for each node, there are several pdf documents attached.
$ query = db_select('node', 'n'); $ query->leftjoin('field_data_krown_weight', 'kw', 'n.nid = kw.entity_id'); $ query->leftjoin('field_data_field_pdf_documents', 'pd', 'n.nid = pd.entity_id'); $ query->leftjoin('file_managed', 'fm', 'pd.field_pdf_documents_fid = fm.fid'); $ query->fields('n',array('title','created','type','nid')); $ query->fields('kw',array('krown_weight_value')); $ query->fields('pd',array('field_pdf_documents_fid')); $ query->fields('fm', ['uri']); $ query->condition('n.type', 'conference_material', '='); $ query->orderBy('kw.krown_weight_value', 'ASC'); $ conference_materials = $ query->execute();
The problem when I do this, is that the nodes are duplicated in the query result, as you see there:
I also tried to use the groupBy() argument, this way:
$ query = db_select('node', 'n'); $ query->leftjoin('field_data_krown_weight', 'kw', 'n.nid = kw.entity_id'); $ query->leftjoin('field_data_field_pdf_documents', 'pd', 'n.nid = pd.entity_id'); $ query->leftjoin('file_managed', 'fm', 'pd.field_pdf_documents_fid = fm.fid'); $ query->fields('n',array('title','created','type','nid')); $ query->fields('kw',array('krown_weight_value')); $ query->fields('pd',array('field_pdf_documents_fid')); $ query->fields('fm', ['uri']); $ query->condition('n.type', 'conference_material', '='); **$ query->groupBy('n.title');** $ query->orderBy('kw.krown_weight_value', 'ASC'); $ conference_materials = $ query->execute();
This way the nodes are no more duplicated, but the problem in this case is that I can’t get all files attached to a node, as you can see there: