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.

Data Model Questions /

Holder page and many_many


Reply


3 Posts   457 Views

Avatar
Spiggley

Community Member, 12 Posts

31 October 2012 at 1:31pm

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.

Avatar
kaanuni

Community Member, 22 Posts

1 November 2012 at 1:36am

Edited: 01/11/2012 1:37am

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?

Avatar
Spiggley

Community Member, 12 Posts

1 November 2012 at 2:11am

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.