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.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Populating data based on page calling popup


Go to End


5 Posts   1506 Views

Avatar
Gene

Community Member, 41 Posts

11 September 2009 at 6:24pm

Edited: 11/09/2009 6:34pm

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

}

Avatar
UncleCheese

Forum Moderator, 4102 Posts

12 September 2009 at 1:15am

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

Avatar
Gene

Community Member, 41 Posts

12 September 2009 at 4:31am

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.

Avatar
UncleCheese

Forum Moderator, 4102 Posts

12 September 2009 at 4:48am

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.

Avatar
Gene

Community Member, 41 Posts

12 September 2009 at 4:52am

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