Jump to:

7940 Posts in 1543 Topics by 946 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Populating data based on page calling popup

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 1031 Views
  • Gene
    Avatar
    Community Member
    41 Posts

    Populating data based on page calling popup Link to this post

    I have 3 classes: BlogHolder, BlogEntry, and BlogCategory. BlogHolder is a parent of BlogEntries. I have a many_many relation on BlogEntry for BlogCategory (and BlogCategory has a belongs_many_many to BlogEntry).

    The problem is I also want to associate a BlogCategory to the specific BlogHolder so each BlogHolder can have a separate set of categories. I'm having trouble passing the BlogEntry to the popup for the BlogCategory so I can save $blogentry->Parent() to a has_one field on BlogCategory.

    Sorry, this is kind of confusing so I'll post some code. The way I'm doing it right now works but seems pretty sketchy. It's probably just late and my brain isn't working...

    class BlogHolder extends Page {
       static $allowed_children = array(
          'BlogEntry'
       );
       
       //...
    }

    class BlogEntry extends Page {
       static $many_many = array(
          'BlogCategories' => 'BlogCategory',
       );
       
       public function getCMSFields() {
          $fields = parent::getCMSFields();
          $categories = new ManyManyDataObjectManager(
             $this,
             'BlogCategories',
             'BlogCategory',
             array(
                'Name' => 'Name',
             ),
             'getCMSFields_forPopup'
          );
          $fields->addFieldToTab("Root.Content.Main", $categories);
       
       //...
    }

    class BlogCategory extends DataObject {
       
       static $db = array(
          'Name' => 'Varchar(255)',
          'URLSegment' => 'Varchar(255)'
       );
       
       static $has_one = array(
          'BlogHolder' => 'BlogHolder',
       );
       
       static $belongs_many_many = array(
          'BlogEntries' => 'BlogEntry',
       );
       
       public function getCMSFields_forPopup() {
          return new FieldSet(
             new TextField('Name')
          );
       }
       
       protected function onBeforeWrite() {
          if (isset($this->record['ctf[sourceID]'])) {
             $entryID = $this->record['ctf[sourceID]'];
             $this->BlogHolderID = DataObject::get_by_id('BlogEntry', $entryID)->Parent()->ID;
          }

          $this->URLSegment = singleton('Page')->generateURLSegment($this->Name);
          parent::onBeforeWrite();
       }
       
       public function CategorySegment() {
          return 'category/' . $this->URLSegment;
       }

    }

  • UncleCheese
    Avatar
    4085 Posts

    Re: Populating data based on page calling popup Link to this post

    Could you be a little more specific about what isn't working?

  • Gene
    Avatar
    Community Member
    41 Posts

    Re: Populating data based on page calling popup Link to this post

    Wow, it was a late night. That was a lot of code for a simple question.

    Is this the best way to get a reference to the calling object in the DataObject: $this->record['ctf[sourceID]']

    Basically, I want to be able to pre-populate some hidden values on the DataObject that pops up.

  • UncleCheese
    Avatar
    4085 Posts

    Re: Populating data based on page calling popup Link to this post

    Honestly, I've never been able to get a solid answer on that. Even the SS guys didn't point me in the right direction.

    I've always relied on $_POST to fetch the formdata in my onBefore/After writes.

    Also, you should consider moving your function to onAfterWrite().. my sense is that your custom sourceID will get overwritten in the write() function.

  • Gene
    Avatar
    Community Member
    41 Posts

    Re: Populating data based on page calling popup Link to this post

    Perfect, thanks. Great module(s) btw. Much appreciated.

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