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.

Data Model Questions /

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

TreeDropdownField and $keyField


Go to End


3 Posts   2749 Views

Avatar
ukjola

Community Member, 3 Posts

14 August 2009 at 12:34pm

Hello.
From the TreeDropdownField API, it's my understanding that the $keyField is the column in the $sourceObject where the data that will be stored will come from. If I do the following:

class HomePage extends Page
{
   function getCMSFields()
   {
      $fields = parent::getCMSFields();
            $fields->addFieldToTab('Root.Content.Buttons', new TreeDropdownField('LinkID', 'Choose a page to Link to:', 'SiteTree', 'URLSegment'));
      return $fields;
   }
}

I would expect that in my table (HomePage), the "LinkID" column will be a varchar that stores the URLSegment. However, when I do a dev/build, the follwoing is created:
Field HomePage.LinkID: created as int(11) not null default '0'

It appears that the build ignores the $keyValue and sticks with the default of "ID". While the form functionality is as expected, when I save, I only get the default 0 in the row (as I would expect based on how the table was created).

I feel like I may be missing a something, I've tried something like this:

class HomePage extends Page
{
   static $db = array(
       'LinkID' => 'Text'
   );
.
.
.

but that just gets ignored.

Any ideas as to what I'm doing wrong?

Cheers.

Avatar
ukjola

Community Member, 3 Posts

14 August 2009 at 1:07pm

Edited: 14/08/2009 1:09pm

After reading through the TreeDropdownField.php code, I did a small experiment and dropped the HomePage(_Live, _versions) tables after the build and recreated them with the URLSegment data type:

CREATE TABLE `HomePage` (
  `ID` int(11) NOT NULL auto_increment,
  `LinkID` varchar(255) character set utf8 default NULL,
  PRIMARY KEY  (`ID`),
  KEY `LinkID` (`LinkID`),
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;

That did the trick and everything worked as expected... of course if I rebuild the db (/dev/build), it flips it back:

Field HomePage.LinkID: changed to int(11) not null default '0' (from varchar(255) character set utf8 collate utf8_general_ci)

And it's broken again. So it looks like TreeDropdownField is doing what its supposed to per the API, but the build is not creating the table right.

Is this a bug in the build? or am I missing something that will tell the build to create column with the URLSegment datatype?

Avatar
ukjola

Community Member, 3 Posts

16 August 2009 at 3:43am

In case anyone is trying to do the same thing... I figured it out by starting from scratch:

   static $db = array(
       'LeftURL' => 'VARCHAR(255)',
   );
   static $has_one = array(
   );
   function getCMSFields() 
   {
      $fields = parent::getCMSFields();
      $fields->addFieldToTab('Root.Content.Buttons', new TreeDropdownField('LeftURL', 'Choose a Left Image Link:', 'SiteTree', 'URLSegment'));
      return $fields;
   }