Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Holder page and many_many

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

Page: 1
Go to End
Author Topic: 413 Views
  • Spiggley
    Avatar
    Community Member
    12 Posts

    Holder page and many_many Link to this post

    2.4 project! Hi, I have a page with a many to many relationship

    static $many_many = array(
       'Others' => 'Other', );

    I have no problem getting the related data for page:

    class MyPage_Controller extends Page_Controller {

    function RelatedOthers() {
    return $this->getManyManyComponents('Others');
    }
    }

    but I am also displaying the last 10 MyPages on a "holder" page:

          public function LatestPages($num=10) {
          $latest = DataObject::get_one("MyHolderPage");
          return ($latest) ? DataObject::get("MyPage", "ParentID = $latest->ID", "Date DESC", "", $num) : false;
       }

    This all works but I need to get "RelatedOthers" info for each of the 10 entries on the holder page. I'm sure you'll all think that's easy, but I am stuck. Any help gratefully recieved.

  • kaanuni
    Avatar
    Community Member
    22 Posts

    Re: Holder page and many_many Link to this post

    I had a similar problem that was driving me crazy. You need to move the RelatedOthers method from the controller to the model. The problem is that the controller gets instanciated once when a page is loaded, for the page that is loaded, but not for each of the pages you are retrieving from the LatestPages method. So when you iterate over those pages you are using the db model and the RelatedOthers method is not defined there.

    I'm not sure you even need a RelatedOthers method. Why can't you just iterate over $others in your template?

  • Spiggley
    Avatar
    Community Member
    12 Posts

    Re: Holder page and many_many Link to this post

    Kaanuni

    You are my hero! It was driving me crazy too. Works like a charm now. Yes you are probably right about iterating over $others, but I simplified my code for my question and changed the names (to protect the innocent) ;-)

    Thanks very much.

    413 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.