Jump to:

3372 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Possible to convert a Page to DataObject?

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

Page: 1
Go to End
Author Topic: 1278 Views
  • TotalNet
    Avatar
    Community Member
    181 Posts

    Possible to convert a Page to DataObject? Link to this post

    Was really hoping there'd be a simple way to exploit existing functionality to do this seeing as Page is a type of DataObject anyway...

    There are a couple of hundred pages (a sub-class of page) I want to convert "in place" to a new sub-class of DataObject to be managed with DataObjectManager ideally or ModelAdmin if not. Essentially removing them from the site tree but still accessible via URLSegment also.

    Anyone know of a "trick" to do this or will I have to export and import them into a new DataObject?

    Cheers

    Rich

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Possible to convert a Page to DataObject? Link to this post

    You could write a simple script in the php (as a subclass of migrationtask) and do something like

    $pages = DataObject::get('BlogEntry'); // get all blog entries

    foreach($pages as $page) {
    $do = new SomeDataObject();
    $do->Title = $page->Title;
    $do->Content = $page->Content;
    // what ever other fields

    $do->write(); // save the dataobject.
    }

    Reference:
    http://doc.silverstripe.org/migrationtask

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: Possible to convert a Page to DataObject? Link to this post

    There's an API method to do this:

    http://api.silverstripe.com/2.4/sapphire/model/DataObject.html#methodnewClassInstance

    DataObject newClassInstance( string $newClassName)

    Create a new instance of a different class from this object's record.

    This is useful when dynamically changing the type of an instance. Specifically, it ensures that the instance of the class is a match for the className of the record. Don't set the DataObject->class or DataObject->ClassName property manually before calling this method, as it will confuse change detection.

    If the new class is different to the original class, defaults are populated again because this will only occur automatically on instantiation of a DataObject if there is no record, or the record has no ID. In this case, we do have an ID but we still need to repopulate the defaults.

    return The new instance of the new class, The exact type will be of the class name provided.
    Parameters:
    string $newClassName - The name of the new class

    Eg:

    $page = DataObject::get_one('Page'); // a single Page
    $newObject = $page->newClassInstance('DataObject');

  • TotalNet
    Avatar
    Community Member
    181 Posts

    Re: Possible to convert a Page to DataObject? Link to this post

    Thanks Willr & Hamish,

    There was I thinking I'd have to do something "hacky" when there are two methods to do it right there! Sweet.

    Might look at a combination of both methods but definitely something I'll be using.

    Cheers,

    Rich

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