Jump to:

23377 Posts in 18296 Topics by 2867 members

General Questions

SilverStripe Forums » General Questions » Import CSV with $belongs_many_many records

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 170 Views
  • kindlemanwill
    Avatar
    Community Member
    67 Posts

    Import CSV with $belongs_many_many records Link to this post

    Hi

    I want to be able to import a csv of locations that belong_many_many services. Both locations and services are dataobjects

    In the locations model admin, there is a csv import function, which seems to understand exactly what i want to do and is helpfully suggesting the csv format i should use. The trouble is - I don't understand what it is telling me:

    Database columns

    Lat
    Lat
    Lng
    Lng
    Address
    Address
    Suburb
    Suburb
    State
    State
    Postcode
    Postcode
    Country
    Country
    Relations

    Services
    Service

    what should the format of my csv be to get a location with a couple of services imported?

    Thanks for any guidance

    Will

  • martimiz
    Avatar
    Forum Moderator
    1068 Posts

    Re: Import CSV with $belongs_many_many records Link to this post

    At this point I don't think you can csv-import many_many relations out of the box... Maybe someone can prove me wrong, but else you could do something like this:

    - in your csv-file create a column called 'Service', containing a comma-separated list of service titles

    - extend the CsvBulkLoader to LocationCsvBulkLoader

    - tell your ModelAdmin to use the new extension:

       private static $model_importers = array(
        'Location' => 'LocationCsvBulkLoader'
       );

    - in your LocationCsvBulkLoader create a function that will add the comma-separated list to each new Location object

       public function linkServicesByTitle(&$obj, $val, $record) {
          $obj->ServiceList = $val;
       }

    Again, in your LocationCsvBulkLoader map the csv-file 'Service' column to your function:

       public $columnMap = array(
          ...
          'Service' => '->linkServicesByTitle'
       );

    Now in your Locations' onAfterWrite() function, you can use $this->ServiceList to collect the Services with the requested title(s), and add them to your many_many relation, using:

    if ($this->ServiceList) {
       ...
       $this->Services()->add($aPageYouFound);
    }

    Of course this goes for existing services. You could probably build on this by creating/writing Services on the fly, but that would be more complex.

  • kindlemanwill
    Avatar
    Community Member
    67 Posts

    Re: Import CSV with $belongs_many_many records Link to this post

    Ok, cool thanks for that. I had a slight feeling it wouldn't do it automatically and then the spec came up.

    I should be able to get it working with those instructions. Thanks very much for your help : )

    170 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.