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 /

[SOLVED] Data object belongs_many_many, no relation after csv import


Reply


2 Posts   730 Views

Avatar
ss69std

Community Member, 2 Posts

5 April 2012 at 10:03am

Edited: 06/04/2012 12:31pm

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.

Avatar
ss69std

Community Member, 2 Posts

6 April 2012 at 3:14pm

Edited: 06/04/2012 3:21pm

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}'"
      );
   }

}