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.

Archive /

Our old forums are still available as a read-only archive.

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

Custom Relation Getters


Go to End


4 Posts   1858 Views

Avatar
jam13

121 Posts

10 October 2007 at 10:43am

I'm trying to do this:

class MyPage extends Page {
...
static $belongs_many_many = array(
'Others' => 'OtherPage'
);
...
function getRedOthers() {
return $this->Others("Colour='Red'");
}
function getBlueOthers() {
return $this->Others("Colour='Blue'");
}
}

but when I use RedOthers and BlueOthers as controls in the same template, they return the same results (decided by whichever comes first in the template).

Is this a bug, or am I missing something?

Avatar
Ingo

Forum Moderator, 801 Posts

10 October 2007 at 1:43pm

yes, this is indeed a bug, exactly here:
http://open.silverstripe.com/browser/modules/sapphire/trunk/core/model/DataObject.php#L737

we've been using some light php-based caching of relations for a while, and forgot to invalidate the cache properly in a case of SQL-filtering. as a quick hack, you can just copy this function into your MyPage.php (not OtherPage.php) and comment out the first line.

i've added a ticket for this: http://open.silverstripe.com/ticket/1490 - should be fixed in the next major release.

Avatar
jam13

121 Posts

10 October 2007 at 9:29pm

Thanks - that works now.

I did also try to create a single function that accepted arguments instead:

function getColourOthers($colour = "Red") {

return $this->Others("Colour='$colour'");

}

and whilst the discrete functions were fixed by removing the cache checking line, this approach still seems to be broken.

Avatar
Ingo

Forum Moderator, 801 Posts

10 October 2007 at 11:45pm

seems pretty straightforward to me, not sure what could go wrong in those two lines...
did you flush your template-cache?

btw, the string passed to the relationship-getter is actually a snippet of SQL, so you have to make sure that its escaped properly:
$SQL_colour = Convert::raw2sql($colour);