Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Multiple Dynamic Page Create & Saving Relationship Objects

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

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

Page: 1
Go to End
Author Topic: 1403 Views
  • Hamish
    Avatar
    Community Member
    712 Posts

    Multiple Dynamic Page Create & Saving Relationship Objects Link to this post

    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.

  • Willr
    Avatar
    Forum Moderator
    5511 Posts

    Re: Multiple Dynamic Page Create & Saving Relationship Objects Link to this post

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

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: Multiple Dynamic Page Create & Saving Relationship Objects Link to this post

    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.

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