Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Custom Relation Getters

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

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

Page: 1
Go to End
Author Topic: 1413 Views
  • jam13
    Avatar
    121 Posts

    Custom Relation Getters Link to this post

    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?

  • Ingo
    Avatar
    Forum Moderator
    801 Posts

    Re: Custom Relation Getters Link to this post

    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.

  • jam13
    Avatar
    121 Posts

    Re: Custom Relation Getters Link to this post

    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.

  • Ingo
    Avatar
    Forum Moderator
    801 Posts

    Re: Custom Relation Getters Link to this post

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

    1413 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.