Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » TreeDropdownField and $keyField

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

Page: 1
Go to End
Author Topic: 1182 Views
  • ukjola
    Avatar
    Community Member
    3 Posts

    TreeDropdownField and $keyField Link to this post

    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.

  • ukjola
    Avatar
    Community Member
    3 Posts

    Re: TreeDropdownField and $keyField Link to this post

    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?

  • ukjola
    Avatar
    Community Member
    3 Posts

    Re: TreeDropdownField and $keyField Link to this post

    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;
    }

    1182 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.