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

same dataobject on multiple pages


Reply

6 Posts   1474 Views

Avatar
janulka

6 January 2010 at 8:26am (Last edited: 6 January 2010 8:28am), Community Member, 80 Posts

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
Pigeon

6 January 2010 at 1:02pm (Last edited: 6 January 2010 2:23pm), Community Member, 243 Posts

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

7 January 2010 at 7:55am Community Member, 80 Posts

Hello,

Thanks a lot for fast reply.

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

Avatar
janulka

14 January 2010 at 2:26am Community Member, 80 Posts

anybody please?

Avatar
zenmonkey

24 January 2010 at 9:42am Community Member, 528 Posts

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

24 January 2010 at 11:57pm Community Member, 80 Posts

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