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.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

SimpleTreeDropdownField makes Problems


Go to End
Reply


10 Posts   2844 Views

Avatar
Andre

Community Member, 126 Posts

12 August 2009 at 3:39am

Hi there, I still have problems with SimpleTreeDropdownFiled and TreeDropdownField inside the Javascript Popup.
I have a collection of the following Class:

class Overview extends DataObject{
      static $db = array (
         'LinkTitle' => 'Text',
         'LinkTeaser' => 'Text'
      );

      static $has_one = array (
         'OverviewTarget' => 'SiteTree'
      );

      public function getCMSFields_forPopup(){
         return new FieldSet(
            new TextField('LinkTitle', "Link Titel:"),
            new TextField('LinkTeaser', "Link Teaser:"),
            new SimpleTreeDropdownField('OverviewTarget', "Link Ziel:", 'SiteTree')
         );
      }
}

The Problem is, that the value from the SimpleTreeDropdownField isn't saved.
I'm trying to read OververviewTarget and OverviewTargetID but both values are empty or always showing the value "1".
I also tried to use the Following line (as needed in TreeDropdoewnFiled)

new SimpleTreeDropdownField('OverviewTargetID', "Link Ziel:", 'SiteTree')


but when using OverviewTargetID insted of OverviewTarget the Dropdown doesn't appear in the Popup.
The same is happening to TreeDropdownField. It doesn't appear when using OverviewTargetID and it's not saving the value when using OverviewTarget.

What am I doing wrong, or is there just a simple error in Silverstripe 2.3.2 / 2.3.3?

Avatar
UncleCheese

Forum Moderator, 4096 Posts

12 August 2009 at 3:53am

It should definitely be OverviewTargetID. Looks like you may have set up your model wrong. What page does the DataObject belong to? If the page has_many Overview, then your overview needs to has_one Page, and that has to be the first entry in your has_one

static $has_one = array (
'ObjectHolder' => 'ObjectHolder',
'OverviewTarget' => 'SiteTree'
);

Avatar
Andre

Community Member, 126 Posts

12 August 2009 at 4:01am

Hi, this is the Model:

<?php

class StartPage extends ExtendedPage {

   public static $has_one = array(
      'LeftImage' => 'Image',
      'RightImage' => 'Image'
   );

   static $has_many = array (
      'Overviews' => 'Overview'
   );

   public function getCMSFields(){
      
      $fields = parent::getCMSFields();
      $fields->addFieldToTab("Root.Content.Images", new ImageField('LeftImage', 'Linkes Bild'));
      $fields->addFieldToTab("Root.Content.Images", new ImageField('RightImage', 'Rechtes Bild'));
      $fields->addFieldToTab('Root.Content.Ueberuns', new DataObjectManager(
         $this, // Controller
         'Overviews', // Source name
         'Overview', // Source class
         array(
            'LinkTitle' => 'LinkTitle',
            'LinkTeaser' => 'LinkTeaser'
         ),
         'getCMSFields_forPopup'
      ));

      return $fields;
   }

}

class StartPage_Controller extends ExtendedPage_Controller {

}

?>

Do you mean, that I should add

static $has_one = array (
'StartPage' => 'StartPage',
'OverviewTarget' => 'SiteTree'
);

to the Overview Object?

Avatar
UncleCheese

Forum Moderator, 4096 Posts

12 August 2009 at 4:16am

Yeah, otherwise there's no database relation. The foreign key goes on the has_one object, not on the has_many object. If you run a build with that code you'll see you get a new field.

Avatar
Andre

Community Member, 126 Posts

12 August 2009 at 4:46am

Well, damn, it works. Many thanks. I was a week chewing on this Problem and in the end it was a stupid small mistake. I realy should work a lot more with Silverstripe to have these Concepts in Mind.

Avatar
inCharge

Community Member, 102 Posts

3 July 2010 at 5:08am

Edited: 03/07/2010 5:11am

I have the exact same problem: MasterPage edits a collection of DetailObjects, which include a caption and a target page ID. The target page value from the SimpleTreeDropdownField isn't saved. I've been over the code again and again but can't spot what I'm doing wrong. Any ideas?

The query looks like this. Note the DetailObjectTargetID field is not updated,

INSERT INTO "DetailObject" ("Created") VALUES (NOW())
SELECT "ID" FROM "DetailObject" WHERE "ID" = 2
UPDATE "DetailObject" SET "ClassName" = 'DetailObject', "Caption" = 'Marker', "MasterPageID" = 11, "LastEdited" = '2010-07-01 15:00:50', "Created" = '2010-07-01 15:00:50' where "ID" = 2

After the query, the data looks like this:

ID    ClassName    Created    LastEdited    Caption    MasterPageID    DetailObjectTargetID
2    DetailObject    2010-07-01 15:00:50    2010-07-01 15:00:50    Marker    11    0

========== MasterPage.php ==========

<?php
/**
* Defines the HomePage page type
*/

class MasterPage extends Page {
   static $db = array(
   );

   static $has_many = array(
      'DetailObjects' => 'DetailObject'
   );

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

      // Note: The array defines the fields shown in the grid
      $tablefield = new DataObjectManager(
         $this,
         'DetailObjects',
         'DetailObject',
         array(
            'Caption' => 'Caption'
            ),
         'getCMSFields_forPopup'
      );
      $fields->addFieldToTab( 'Root.Content.Detail', $tablefield );

      return $fields;
   }
}

class MasterPage_Controller extends Page_Controller {

   public function init() {
      parent::init();
   }
}
?>

========== DetailObject.php ==========

<?php

class DetailObject extends DataObject {

   public static $db = array(
      'Caption' => 'Text'
   );

   public static $has_one = array(
      'MasterPage' => 'MasterPage',
      'DetailObjectTarget' => 'SiteTree'
   );

   // Define the fields shown in the record editor popup
   public function getCMSFields_forPopup() {
      return new FieldSet(
         new TextField('Caption'),
         new SimpleTreeDropdownField('DetailObjectTargetId', "Target page", 'SiteTree')
      );
   }
}
?>

Cheeky 2nd question: Does DOM work with TreeDropdownField? I'm concerned about scalability - if the site has too many pages then SimpleTreeDropdownField may become unusable.

Thanks,
Jules

Avatar
UncleCheese

Forum Moderator, 4096 Posts

3 July 2010 at 5:31am

DetailObjectTargetId

Don't you mean DetailObjectTargetID?

Avatar
inCharge

Community Member, 102 Posts

6 July 2010 at 8:22am

Edited: 06/07/2010 8:28am

Nice one boss, that's it! Thanks!

Is there any way of getting warning/error messages when I make that kind of mistake? SilverStripe seems to fail quietly - either ignoring errors or generating a blank screeen, even with &isDev=1.

Go to Top