3063 Posts in 864 Topics by 646 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 1136 Views |
-
Possible to convert a Page to DataObject?

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
-
Re: Possible to convert a Page to DataObject?

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 -
Re: Possible to convert a Page to DataObject?

22 June 2010 at 9:33pm Last edited: 22 June 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'); -
Re: Possible to convert a Page to DataObject?

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
| 1136 Views | ||
|
Page:
1
|
Go to Top |


