7912 Posts in 1355 Topics by 930 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, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w
|
Page:
1
|
Go to End | |
| Author | Topic: | 908 Views |
-
Populating data based on page calling popup

11 September 2009 at 6:24pm Last edited: 11 September 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;
}}
-
Re: Populating data based on page calling popup

12 September 2009 at 1:15am
Could you be a little more specific about what isn't working?
-
Re: Populating data based on page calling popup

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.
-
Re: Populating data based on page calling popup

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.
-
Re: Populating data based on page calling popup

12 September 2009 at 4:52am
Perfect, thanks. Great module(s) btw. Much appreciated.
| 908 Views | ||
|
Page:
1
|
Go to Top |

