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.

Data Model Questions /

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

Possible to convert a Page to DataObject?


Go to End


4 Posts   2183 Views

Avatar
TotalNet

Community Member, 181 Posts

22 June 2010 at 11:18am

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

Avatar
Willr

Forum Moderator, 5523 Posts

22 June 2010 at 7:29pm

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

Avatar
Hamish

Community Member, 712 Posts

22 June 2010 at 9:33pm

Edited: 22/06/2010 9:35pm

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

Avatar
TotalNet

Community Member, 181 Posts

23 June 2010 at 9:24am

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