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.

General Questions

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

SS3.1 - Problem with GridField one-to-one relationship


Reply

208 Views

Avatar
dylangrech92

10 March 2014 at 9:41am Community Member, 17 Posts

I have two classes which I want to have a one-to-one relationship between them and be able to manage everything through GridField however GridField throws an error: [User Error] Uncaught LogicException: GridField doesn't have a modelClassName, so it doesn't know the columns of this grid.

These are my classes:
// Channel Page
class ChannelPage extends Page{
   private static $can_be_root = false;
   private static $has_one      = array( 'Image' => 'Photo' );
   
   public function getCMSFields(){
      $fields = parent::getCMSFields();
      
      $config = GridFieldConfig_RelationEditor::create();
$config->getComponentByType( 'GridFieldDataColumns' )->setDisplayFields( array(
         'Name'     => 'Name',
         'Description' => 'Description'
      ) );
      
$fields->addFieldToTab( 'Root.Image', new GridField( 'Image', 'Image', $this->Image(), $config ) );
      
      return $fields;
   }
}

// PhotoDecorator
class PhotoDecorator extends DataExtension{
   static $has_one = array(
      'IndexPage'    => 'IndexPage',
      'ChannelPage'   => 'ChannelPage'   
   );
   
   public function updateCMSFields( FieldList $fields ){
      $fields->removeByName( 'IndexPageID' );
      $fields->removeByName( 'ChannelPageID' );
   }
}

// Photo
class Photo extends DataObject{
   private static $db = array(
      'Name'        => 'VARCHAR(255)',
      'Description' => 'Text'
   );
   
   private static $has_one = array( 'Image' => 'Image' );
}