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.

Customising the CMS /

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

sourceFilter and sourceSort not working in ComplexTableField? (2.4.0)


Go to End


1601 Views

Avatar
Laurie

Community Member, 21 Posts

2 June 2010 at 2:41pm

I'm trying to include a "highlights" box (think announcements or news items) on several pages on my site.

The rules are that...
- A highlight may appear only on the home page.
- A highlight may appear on the home page and one or more practice area pages.
- A highlight may appear on one or more practice area pages without appearing on the home page.

I have created a Highlight class which extends DataObject.

As part of that class there is a boolean variable for whether or not the item should appear on the home page.

In addition a highlight has a many-to-many relationship with the different practice areas included on the site.

For example, if we were to win a contract dealing with gender equity in education an announcement of that contract might appear on the home page, the gender page, and the education page.

For each applicable page type (i.e., home and practice area) I would like the highlights tab in the CMS to only show highlights that will appear on the particular page. So, I need to filter all highlights for just the applicable highlights on any given page.

My understanding of how to do this is to include the $sourceFilter and $sourceSort as part of my ComplexTableField.

However, it seems like $sourceSort is being used for the WHERE clause of the query and $sourceSort is being totally ignored.

Here is the code I'm using to try and create the tab as desired:

/mysite/code/HomePage.php

<?php
class HomePage extends Page {
static $db = array (
);

function getCMSFields() {
$fields = parent::getCMSFields();
$highlightsTable = new ComplexTableField (
$controller = $this,
$name = 'HomePageHighlights',
$sourceClass = 'Highlight',
$fieldList = array(
'Headline'=>'Headline',
'StartDate'=>'Start Date',
'EndDate'=>'EndDate'
),
$sourceFilter = 'HomePage = 1',
$sourceSort = 'startDate DESC'
);
$fields -> addFieldToTab('Root.Content.Highlights', $highlightsTable);
return $fields;
}
}
class HomePage_Controller extends Page_Controller {
function HomePageHighlights() {
$now = date('Y-m-d');
$where = "startDate <= '$now' AND endDate > '$now' AND homePage = 1";
return DataObject::get('Highlight', $where, 'startDate DESC');
}
}
?>

However, this is producing the following error in my SilverStripe log:

[01-Jun-2010 09:18:32] Error at \sapphire\core\model\MySQLDatabase.php line 536: Couldn't run query:
SELECT "Highlight"."ClassName", "Highlight"."Created", "Highlight"."LastEdited", "Highlight"."Headline", "Highlight"."Blurb", "Highlight"."Link", "Highlight"."StartDate", "Highlight"."EndDate", "Highlight"."HomePage", "Highlight"."ID", CASE WHEN "Highlight"."ClassName" IS NOT NULL THEN "Highlight"."ClassName" ELSE 'Highlight' END AS "RecordClassName"
FROM "Highlight"
WHERE (startDate DESC)

Based on the error it looks like $sourceSort is being used where $sourceFilter should appear in the WHERE clause and no ORDER BY clause is being included at all.

Any insight would be most appreciated. Thanks.

Cheers,
Laurie