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.


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

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

Custom Relation Getters


4 Posts   1427 Views


10 October 2007 at 10:43am 121 Posts

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?


10 October 2007 at 1:43pm Forum Moderator, 801 Posts

yes, this is indeed a bug, exactly here:

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: - should be fixed in the next major release.


10 October 2007 at 9:29pm 121 Posts

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.


10 October 2007 at 11:45pm Forum Moderator, 801 Posts

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);