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

Convert Array to DataObject


Go to End


6 Posts   5499 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, 1899 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 :)