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.

Data Model Questions

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


Reply

2 Posts   952 Views

Avatar
Garrett

1 March 2012 at 5:52am Community Member, 245 Posts

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

Avatar
rob.s

4 March 2012 at 6:18am Community Member, 78 Posts

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