Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

All other Modules /

Discuss all other Modules here.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

DataQuery: Add parent field


Go to End


943 Views

Avatar
Ronaldo71

Community Member, 10 Posts

7 August 2014 at 12:29am

Hi all,

I need to configure a DataList with a bunch of child records, including some of the fields of it's many-to-one parent relation. If possible, I'd prefer to include that parent data using a left join in one sweep, instead of using multiple queries to fetch a parent for each child.

I've tried to change the query/DataQuery of an existing DataList (created by a SearchContext elsewhere) to include a parent field.
So far, I've:

$dataList = $dataList->alterDataQuery(
    function($dataQuery) {
        $dataQuery->applyRelation('Parent');
        $dataQuery->setQueriedColumns(['ID', 'OtherChildField', 'Parent.Name']);
        echo $dataQuery->sql();
    }
);

The applyRelation method perfectly adds a left join, and I even can use where statements based on fields of the parent.
But what do I miss to include the Parent.Name field in the SELECT clause?
Having traced through the code, it seems like the dataQuery (or it's query) doesn't allow me to include fields other that the model's (child) own fields...

Even though the DataList->getFinalisedQuery has a parameter to pass custom fields, the DataList->sql() method doesn't provide this parameter, and the getFinalisedQuery gets to decide which fields are allowed, disregarding all fields that don't belong in the DataList's model.

I'd appreciate any Idea's :)
Ronald