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.

Archive

Our old forums are still available as a read-only archive.

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

has_one relations. Get title from ID in the template


Reply

4 Posts   2067 Views

Avatar
tipylis

1 November 2007 at 1:58pm 2 Posts

Hi,

i just started to play around SS. I have a little problem which, i guess, can be easy solved.

I install countries, which i can chose in the admin system, but i can't get this country name in the templates, because i got just it ID from variable $CountryID.

Any ideas how to declarate country title from it ID.

class Lake extends Lakes {
   static $db = array(
   'CountryID' => 'Int'
   );
   static $has_one = array(
   'Countries' => 'Country'
   );

   function getCMSFields() {

      $fields = parent::getCMSFields();

      $fields->addFieldToTab('Root.Content.Country',new DropdownField('CountryID','Select one',DataObject::get('Country')->toDropDownMap('ID', 'Title')));
      return $fields;
   }

}

Thank you,
Vitas

Avatar
Nathan Cox

1 November 2007 at 4:12pm (Last edited: 1 November 2007 4:12pm), Community Member, 99 Posts

If I understand correctly, you can put something like this in your LakeController class:

   public function Country() {
      $country= $this->getComponent('Country');
      return $country;
   }

Then in the template, use $Country.Title to get the country's name.

I think.

Avatar
Sean

1 November 2007 at 4:56pm (Last edited: 1 November 2007 4:57pm), Forum Moderator, 921 Posts

You shouldn't need to add 'CountryID' as a $db array entry. The has_one automatically makes a CountryID for you I thought.

I think the way you've named has_one = array('Countries' => 'Country') is confusing, I would've made it 'Country' => 'Country' as it's only one component, and not more than one.

You should just be able to call $Countries in the template, or $this->Countries() in the code on the controller to return your country object (with the current way you've got it setup). Is that not the case?

Hope this helps,

Cheers,
Sean

Avatar
tipylis

2 November 2007 at 1:34pm (Last edited: 3 November 2007 12:19am), 2 Posts

Thank You Sean for your advice.

Looks i solved the problem for now, but I guess that this way is not correct. I think the problem still exist because I was not able to use any suggested code in this forum.

I will be interesting to know other ways how to solve this problem. Please, let me know.

Currently working code:

<?php

class Lake extends Lakes {
static $db = array(
);
static $has_one = array(
'Countries' => 'Country'
);

function getCMSFields() {

$fields = parent::getCMSFields();

$fields->addFieldToTab('Root.Content.Country',new DropdownField('CountryID','Select one',DataObject::get('Country')->toDropDownMap('ID', 'Title')));
return $fields;
}

}

class Lake_Controller extends Lakes_Controller {

   public function Country() {

      $country = DataObject::get_one("Country", "`Country_Live`.ID = ".$this->__get('CountryID'));
      
      return ($country)?$country->__get('Title'):false;
   }

}

?>

This code should not be like i described in the controller, but i think that here still is bug. I found one link, where was described problem about dropdown then CMS do not save selections. I guess this kind of code is temporary.