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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

Possibility to have hasmany relation to be different on every page?


Go to End


2 Posts   686 Views

Avatar
quanto

Community Member, 91 Posts

29 September 2011 at 8:06pm

i'm trying to get a Tab "Fotos" Wherein the enduser has the possibility to upload thumbnails and photo's. The upload module works, except every page shows the same pictures. I think it's in the fotos.php static has_one -> SiteTree, because of it stays 0 in the db after saving.

I want to have every page different pictures (eg home: 1.jpg, 2.jpg; contact: 3.jpg, 4.jpg),

Foto's.php:

<?php
/**
 * Defines the Fotos
 */
class Fotos extends DataObject {
  static $db = array(
      'SmallCapt' => 'Text',
      'LargeCapt' => 'Text'
  );
  
  static $has_one = array(
    'SmallFoto' => 'Image',
    'LargeFoto' => 'Image',
    'MySiteTree' => 'SiteTree'
  );

  function getCMSFields_forPopup() {
    $fields = new FieldSet();
     
    $fields->push( new SimpleImageField( 'SmallFoto', 'Kleine foto') );
    $fields->push( new Textfield( 'SmallCapt', 'Beschr. kleine foto' ) );
    $fields->push( new SimpleImageField( 'LargeFoto', 'Grote foto' ) );
    $fields->push( new Textfield( 'LargeCapt', 'Beschtr. grote foto' ) );
     
    return $fields;
  }
}
?>

Page.php

...
  public static $has_many = array(
    'MyFotos' => 'Fotos'
	);
  
  function getCMSFields() {
    $fields = parent::getCMSFields();
    $tablefield = new HasManyComplexTableField(
        $this,
        'MyFotos',
        'Fotos',
        array(
            'SmallFoto' => 'Kleine foto',
            'SmallCapt' => 'Beschrijving',
            'LargeFoto' => 'Grote foto',
            'LargeCapt' => 'Beschrijving'
        ),
        'getCMSFields_forPopup'
    );
         
    $fields->addFieldToTab('Root.Content.Fotos', $tablefield );
    return $fields;
  }
...

Avatar
zenmonkey

Community Member, 545 Posts

30 September 2011 at 4:04am

The Fotos Object needs to have a $has_one relation with Page, not SiteTree.

As an aside you may want to look at the DataObjectManager module instead of using a HasManyComplexTableField since it has good image relation manager ImageDataObjectManager