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.

Data Model Questions /

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

same dataobject on multiple pages


Go to End


6 Posts   2272 Views

Avatar
janulka

Community Member, 80 Posts

6 January 2010 at 8:26am

Edited: 06/01/2010 8:28am

Hello,

I am updating website for a festival, and I want to use page ProgramDay.php to store several concerts Concert.php as Dataobject.

It is obvious that ProgramDay 1 has different concerts than ProgramDay 2

My problem is that same concerts appear on EACH ProgramDay page, when I add concerts to ProgramDay 1, they appear also on Program Day 2..

Website is built with SS 2.2.2

Please help? Thanks in advance

Concert.php

<?php

class Concert extends DataObject {

	
	static $db = array (
		'Title' => 'Text',
		'Place' => 'Text',
		);
	
	static $has_one = array (
		'ProgramDay' => 'ProgramDay'
		);	
	
   function getCMSFields_forPopup() {
      $fields = new FieldSet();
      $fields->push( new TextField( 'Title', 'Concert title' ) );
      $fields->push( new TextField( 'Place' ) );
      return $fields;
   }


}
?>

ProgramDay.php

<?php


class ProgramDay extends Page {
 
  
   
   static $has_many = array (
		'Concerts' => 'Concert'
		);
   
		
	function getCMSFields() {
      $fields = parent::getCMSFields();
	  
 
      $tablefield = new ComplexTableField(
         $this, 'Concerts', 'Concert',
         array( 'Title' => 'Title', 'Place' => 'Place'), 'getCMSFields_forPopup' );
      $tablefield->setParentClass('ProgramDay');
 
      $fields->addFieldToTab( 'Root.Content.Concerts', $tablefield );
	  
      return $fields;
   }

}
 
class ProgramDay_Controller extends Page_Controller {
	
	function Siblings() {
  $whereStatement = "ParentID = ".$this->ParentID;
  return DataObject::get("Page", $whereStatement);
 }

 
}
 
?>

Avatar
dhensby

Community Member, 253 Posts

6 January 2010 at 1:02pm

Edited: 06/01/2010 2:23pm

Hi janulka,

Seems like the forum is picking up again after the Christmas break.

Right, your problem is an interesting one and one i have had a few times and wanted to get past. But i haven't had a good excuse until now. After doing some digging in the source i have found out how to fix this problem (in theory :P).

ProgramDay.php:

<?php

class ProgramDay extends Page {

static $has_many = array ( 
      'Concerts' => 'Concert' 
      );

       
   function getCMSFields() { 
$fields = parent::getCMSFields(); 
   

$tablefield = new ComplexTableField( 
$this, 'Concerts', 'Concert', 
array( 'Title' => 'Title', 'Place' => 'Place'), 'getCMSFields_forPopup' ); 
$tablefield->setParentClass('ProgramDay');
$concerts = DataObject::get('Concert','ProgramDayID = ' . $this->ID);
$tablefield->setCustomSourceItems($concerts);

$fields->addFieldToTab( 'Root.Content.Concerts', $tablefield ); 
    
return $fields; 
}

}

class ProgramDay_Controller extends Page_Controller { 
    
   function Siblings() { 
$whereStatement = "ParentID = ".$this->ParentID; 
return DataObject::get("Page", $whereStatement); 
}

}

Simply, what it does is fetches our own DOS that we want to have shown in the complextablefield and pushes it in. So i have chosen to show only the objects that belong to the current page.

Hope that is what you were looking for.

Avatar
janulka

Community Member, 80 Posts

7 January 2010 at 7:55am

Hello,

Thanks a lot for fast reply.

This did not do it, unfortunately, still same problem..

Avatar
janulka

Community Member, 80 Posts

14 January 2010 at 2:26am

anybody please?

Avatar
zenmonkey

Community Member, 545 Posts

24 January 2010 at 9:42am

Have you checked the Database to make sure the Concert has the correct ProgramDayID attached?

If it does make sure your Concert getter in ProgramDay_Controller is forming the query correctly

public function getConcerts(){
return DataObject::get("Concert","ProgramDayID =".$this->ID);
}

Then in your template just call <% control getConcerts %>

Avatar
janulka

Community Member, 80 Posts

24 January 2010 at 11:57pm

Thanks for reply!

I gave up on this one, and upgraded SS on the site to 2.3.4. and installed DataObjectManager, since then everything is alright. I never understood why this was happening..