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.

Archive /

Our old forums are still available as a read-only archive.

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

Multiple Dynamic Page Create & Saving Relationship Objects


Go to End


3 Posts   1903 Views

Avatar
Hamish

Community Member, 712 Posts

11 September 2008 at 3:18pm

Two issues, both which I thought should be straight forward.

1. I'm trying to create an import data function.

I've added a field in the CMS where the user can enter data. On saving the page, the onBeforeWrite function finds that data and attempts to create sub pages based on what was entered.

However, it seems that writing a page in the onBeforeWrite of another page stops the rest of the page creations, and stops and javascript execution.

Anyone have an idea why this is happening, or have a better method?

2. This one is really dumb, but in the same script, I'm trying to add other objects to the pages that I'm creating. The new page is a subclassed object with two has_one relationships. How can I add these objects while I'm saving these generated pages?

Eg:

$newPage->Title = "New Page 1";
$newPage->someObject = new customDataObject();
$newPage->write();

But the 'someObjectID' in the generated page is always zero.

Clues?

Cheers.

Avatar
Willr

Forum Moderator, 5523 Posts

11 September 2008 at 4:40pm

Edited: 11/09/2008 4:41pm

Im not sure how you have done it but it would seem to me as a wee bit recursive eg on before writing this write X but if X is the same page type as the first one then does that call onBeforeWrite and does that go off and create an object which calls that same onBeforeWrite.. and so on and so on......

Also if thats not the case make sure in your onBeforeWrite you call parent::onBeforeWrite() so that everything else works.

For #2 try this

$newPage->Title = "New Page 1"; 
$someObject = new customDataObject(); 
$someObject->Property = "Wow this is a Field on this Object";
$someObject->write();
$newPage->someObjectID = $someObject->ID
$newPage->write();

Avatar
Hamish

Community Member, 712 Posts

11 September 2008 at 5:06pm

Hey Will, yeah the recursion issue occured to me and I made sure that the parent call was first - although it will be great when onAfterWrite is released - no more problems :)

I've found a much better method, using an InlineFormAction field, calling a controller and a director rule in _config.php. Much better solution - I'll post it publicly tomorrow.

Cheers.