Jump to:

3448 Posts in 1062 Topics by 738 members

Data Model Questions

SilverStripe Forums » Data Model Questions » [SOLVED] Data object belongs_many_many, no relation after csv import

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

Page: 1
Go to End
Author Topic: 641 Views
  • MattIn4D
    Avatar
    Community Member
    2 Posts

    [SOLVED] Data object belongs_many_many, no relation after csv import Link to this post

    I have a data object that contains tournament standings.

    class Standing extends DataObject
    {
       static $db = array (
          'Place' => 'Text',
          'Prize' => 'Text',
          'Bowler' => 'Text',
          'Series' => 'Text',
          'Average' => 'Text',
          'Handicap' => 'Text',
          'Game1' => 'Text',
          'Game2' => 'Text',
          'Game3' => 'Text',
          'Game4' => 'Text'
       );

       static $summary_fields = array (
          'Place' => 'Place',
          'Prize' => 'Prize',
          'Bowler' => 'Bowler',
          'Series' => 'Series',
          'Average' => 'Average',
          'Handicap' => 'Handicap',
          'Game1' => 'Game1',
          'Game2' => 'Game2',
          'Game3' => 'Game3',
          'Game4' => 'Game4',
          'Season' => 'Season'
       );
       
       static $belongs_many_many = array(
          'Season' => 'SeasonPage'
       );

       static $searchable_fields = array (
          'Season.ID' => array(
             'title' => 'Season'
          )
       );

       function getCMSFields()
       {
          $fields = parent::getCMSFields();

          $fields->addFieldToTab("Root.Main", new TextField('Place', 'Place'));
          $fields->addFieldToTab("Root.Main", new TextField('Prize', 'Prize'));
          $fields->addFieldToTab("Root.Main", new TextField('Bowler', 'Bowler'));
          $fields->addFieldToTab("Root.Main", new TextField('Series', 'Series'));
          $fields->addFieldToTab("Root.Main", new TextField('Average', 'Average'));
          $fields->addFieldToTab("Root.Main", new TextField('Handicap', 'Handicap'));
          $fields->addFieldToTab("Root.Main", new TextField('Game1', 'Game1'));
          $fields->addFieldToTab("Root.Main", new TextField('Game2', 'Game2'));
          $fields->addFieldToTab("Root.Main", new TextField('Game3', 'Game3'));
          $fields->addFieldToTab("Root.Main", new TextField('Game4', 'Game4'));
          
          // Season
          $Season = DataObject::get('SeasonPage');
          $fields->addFieldToTab("Root.Season", new CheckboxsetField('Season', 'Season', $Season));

          return $fields;
       }
    }

    It belongs many many to SeasonPage.

    class SeasonPage extends Page
    {
       static $many_many = array(      
          'Standings' => 'Standing'
       );

       static $allowed_children = array(
          'none' => 'none'
       );
    }

    class SeasonPage_Controller extends Page_Controller
    {
       //Return the list of standings for this season
       public function getStandingsList()
       {
          return $this->Standings(Null, 'Place ASC');
       }
    }

    What I need is to upload a csv file containing each seasons standings. This seems to work fine except that the standings are not relating to a SeasonPage. If I manually relate the standings to a season then export a csv it shows 2012[] in the Season column. But if I try to import a csv with 2012[] in the Season column the standings don't relate to a SeasonPage.
    Any help would be greatly appreciated.

  • MattIn4D
    Avatar
    Community Member
    2 Posts

    Re: [SOLVED] Data object belongs_many_many, no relation after csv import Link to this post

    I think I solved this.

    I changed the relationships to has_one & has_many.

    I added the following to the model admin extension:
       static $model_importers = array(
          'Standing' => 'StandingCsvBulkLoader',
       );

    And extended CsvBulkLoader like so:
    class StandingCsvBulkLoader extends CsvBulkLoader {

       public $columnMap = array(
          'Place' => 'Place',
          'Bowler' => 'Bowler',
          'Series' => 'Series',
          'Average' => 'Average',
          'Handicap' => 'Handicap',
          'Game1' => 'Game1',
          'Game2' => 'Game2',
          'Game3' => 'Game3',
          'Game4' => 'Game4',
          'Season' => 'Season.Title'
       );

       public $relationCallbacks = array(
          'Season.Title' => array(
             'relationname' => 'Season',
             'callback' => 'getSeasonByTitle'
          )
       );

       static function getSeasonByTitle(&$obj, $val, $record) {
          $SQL_val = Convert::raw2sql($val);
          return DataObject::get_one(
             'SeasonPage', "Title = '{$SQL_val}'"
          );
       }

    }

    641 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.