17488 Posts in 4473 Topics by 1978 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 1755 Views |
-
has_one relations. Get title from ID in the template

1 November 2007 at 1:58pm
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 -
Re: has_one relations. Get title from ID in the template

1 November 2007 at 4:12pm Last edited: 1 November 2007 4:12pm
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.
-
Re: has_one relations. Get title from ID in the template

1 November 2007 at 4:56pm Last edited: 1 November 2007 4:57pm
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 -
Re: has_one relations. Get title from ID in the template

2 November 2007 at 1:34pm Last edited: 3 November 2007 12:19am
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.
| 1755 Views | ||
|
Page:
1
|
Go to Top |



