Jump to:

3373 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » same dataobject on multiple pages

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

Page: 1
Go to End
Author Topic: 1402 Views
  • janulka
    Avatar
    Community Member
    80 Posts

    same dataobject on multiple pages Link to this post

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

    }

    ?>

  • Pigeon
    Avatar
    Community Member
    243 Posts

    Re: same dataobject on multiple pages Link to this post

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

    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.

  • janulka
    Avatar
    Community Member
    80 Posts

    Re: same dataobject on multiple pages Link to this post

    Hello,

    Thanks a lot for fast reply.

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

  • janulka
    Avatar
    Community Member
    80 Posts

    Re: same dataobject on multiple pages Link to this post

    anybody please?

  • zenmonkey
    Avatar
    Community Member
    524 Posts

    Re: same dataobject on multiple pages Link to this post

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

  • janulka
    Avatar
    Community Member
    80 Posts

    Re: same dataobject on multiple pages Link to this post

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

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