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

Pre-loading a data store with initial data


Reply

3 Posts   1304 Views

Avatar
Judge

22 February 2010 at 1:24am (Last edited: 22 February 2010 1:37am), Community Member, 79 Posts

Is there a way to automatically load up a data store (a datamodel table) with information when it is created? I require a country-selection field and figured it would be good idea to put it into a separate module for reuse. The countries would be stored in the database, and so it would be ideal for the table to be pre-populated as soon as the module is installed.

The country data will be in XML files initially, to make it easier to handle international characters.

Unless, of course, such a module already exists?

-- Jason

Edit: of course, an ideal module would contain an arbitrary number of nested and linked regions of any arbitrary type, for use in different ways. For example, to be able to define the UK, its countries, then its regions, then its counties (of which there are several sets, depending on how you define "county"), and its postal code areas (overlapping and not fully enclosed within counties) etc. would be tremendously useful. That would also link to GIS outline shapes, yet another level of usefulness. That is probably a step further than I need to go now, especially being a newbie to SS.

Avatar
Hamish

22 February 2010 at 8:36am Community Member, 712 Posts

Hiya,

If you add the method requireDefaultRecords() to your model class, it will be called when the database is built, after the tables have been created.

I.e. you could write a script that loads data from an XML file when your database is created. Note that Sapphire comes with Spyc for reading YAML documents - this could be a good way to load up a simple dataset like regions.

Incidentally, I've already done something similar to your arbitrary linked regions system for a project. Using the [url]http://doc.silverstripe.org/doku.php?id=hierarchy[/url] extension it's quite easy to implement. We haven't gone quite as far as added a GIS layer over the top (partly because the regions were not always strictly geographic), but it's quite achievable.

Avatar
Judge

22 February 2010 at 8:58am Community Member, 79 Posts

Thanks Hamish,

The GIS layer I was thinking of probably doesn't involve much more than a large text field to hold CSV polygon data for the outlines (more likely multiple polygons, so broken areas and holes in areas can be drawn). I've done something similar in another project, with the polygon data being pulled out and converted into Google Maps polygon layers with roll-overs. There was no need to split the individual points out into separate data items, so storing it is very easy.

I'll have a look at YAML. It's a term I've seen a lot, but never really explored it.

-- Jason