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

Gridfield with relationEditor has_one Dataobject


Reply

5 Posts   1159 Views

Avatar
Matulko

29 June 2012 at 8:05pm Community Member, 9 Posts

hello
I got few guestions about gridfield and creating new dataobject there

I want create photogallery for article pages so I got this code

class ArticlePhoto extends DataObject
{
   public static $singular_name = 'Obrázok k článku';
   
   public static $db = array (
      'Title' => 'text'
   );
   
   public static $has_one = array (
      'Image' => 'Image',
      'Page' => 'Page',
      );
}

class Article extends Page
{

   public static $has_many = array(
      'ArticlePhotos' => 'ArticlePhoto'
   );
   
   public function getCMSFields()
   {
      $f = parent::getCMSFields();
      
      $dl = DataList::create('ArticlePhoto')
         ->where('PageID ='.$this->ID);
      
      $config = new GridFieldConfig_RelationEditor();
            
      $gridField = new GridField('ArticlePhotos','ArticlePhotoGallert', $dl,$config);
            
      $f -> addFieldToTab('Root.PhotoGallery', $gridField);
      return $f;
   }
}

I want use this DataObject for more page subclasses so for that is is in has_one Page class

it looks like really simple code but my probles is how to set automatically dataobject has_one page

when I create new dataobject this is not automatically set and I am redirected to page with ID = 1
when I delete filter '->where('PageID ='.$this->ID);' everything works fine but I still have to set this relation

I want just create DataObject and then upload Image without set this relation
any suggestions???

Avatar
elgordo

10 February 2013 at 12:45am Community Member, 70 Posts

Noticed a typo in your code, ArticlePhotoGallert - should this be ArticlePhotoGallery?

Avatar
elgordo

10 February 2013 at 12:45am Community Member, 70 Posts

Noticed a typo in your code, ArticlePhotoGallert - should this be ArticlePhotoGallery?

Avatar
SamTheJarvis

26 February 2013 at 4:30am (Last edited: 26 February 2013 4:34am), Community Member, 24 Posts

This is a problem I have too. There doesn't seem to be any solution for adding/editing/deleting objects that are attached through a has_one.

Sorry I can't help, I'm looking into a way to do this.

Avatar
Matulko

26 February 2013 at 8:43pm (Last edited: 26 February 2013 8:44pm), Community Member, 9 Posts

thanks for help
i started using multiple upload field for this