Jump to:

3448 Posts in 1062 Topics by 738 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Page Relations: $has_one Using Dropdown Map - Not Saving to Field

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

Page: 1
Go to End
Author Topic: 918 Views
  • Garrett
    Avatar
    Community Member
    245 Posts

    Page Relations: $has_one Using Dropdown Map - Not Saving to Field Link to this post

    Hi,

    I am struggling getting a relationship set up in my database. Was hoping some gurus might be able to help a brutha out. Here's the deal. I have a Client pagetype:

    class Client extends Page {
    .....
    static $db = array(
          "Sectors" => "Text",
          "IsActive" => "Text",
          "ExternalURL" => "Text"
       );
       
       static $has_one = array(
          "ClientHolder" => "ClientHolder",
          "Logo" => "Image"
       );
    .....
    }

    Then I have a NewsPage pagetype which contains a Client field:

    class NewsPage extends Page {
    .....
    static $has_one = array(
    "Client" => "Client"
    );
    .....


    This creates a field "ClientID" in the NewsPage table, as expected. Now, in order to select the related client on a News Page in the CMS, I create a dropdown of the available Client objects in the system:

    $clients = Page_Controller::GetClients();
    $client_dropdownfield = new DropdownField("ClientID", "Client", $clients->toDropDownMap("Title", "Title"),null,null,"");
    $fields->addFieldToTab("Root.Content.Main", $client_dropdownfield, "Content");

    ...And the function that gets the Clients:

    class Page_Controller extends ContentController {
    .....
    public function GetClients() {
    return DataObject::get("Client", "","Title ASC","","");
    }
    .....

    However, when I save a NewsPage, the ClientID is always 0. It is not saving anything in this field. What am I doing wrong? I thought this would behave just like when you save a LinkedPageID from a SimpleTreeDropdownField. How do I create this relation properly?

    Thanks,
    Garrett

  • rob.s
    Avatar
    Community Member
    78 Posts

    Re: Page Relations: $has_one Using Dropdown Map - Not Saving to Field Link to this post

    Hi Garret,

    try to change the field name from ClientID to Client:

    $client_dropdownfield = new DropdownField("Client", "Client", $clients->toDropDownMap("Title", "Title"),null,null,"");

    because the named relation is "Client"

    Works?

    Rob

    918 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.