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.

Data Model Questions /

Convert Array to DataObject


Reply


6 Posts   3126 Views

Avatar
biapar

Forum Moderator, 435 Posts

29 September 2010 at 10:53am

Hi,

I've build an array from reading xml file via SimpleXML. So, I want to use that data into template.

How convert array to dataobject?

How save data array to database, thus I've data into db and I made query on it ( more faster then parser remote xml file )?

Thank you

Avatar
Martijn

Community Member, 271 Posts

29 September 2010 at 11:07am

I found this a nice example how to convert a XML Feed to a DataObjectSet:

http://www.i-lateral.com/tutorials/silverstripe-dataobjectset-and-templates/

You can use that example to store the array to a DataObject.

foreach ($someArray as $item){
$obj = new SomeDataObject();
$obj->Title = $item['Title'];
$obj->write();
}

Avatar
biapar

Forum Moderator, 435 Posts

29 September 2010 at 8:25pm

What Will be SomeDataObject? A class?

Avatar
Martijn

Community Member, 271 Posts

29 September 2010 at 10:23pm

Any DataObject you use for strorage.

class MyItem extends DataObject{}

Avatar
swaiba

Forum Moderator, 1799 Posts

30 September 2010 at 12:37am

if your array contains the correct key => data then you can simply do...

$doMyDataObject = new MyDataObject($data);
$doMyDataObject->write();

Avatar
Martijn

Community Member, 271 Posts

30 September 2010 at 12:44am

Ah, thats nice to know :)