Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » has_one relations. Get title from ID in the template

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

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

Page: 1
Go to End
Author Topic: 1956 Views
  • tipylis
    Avatar
    2 Posts

    has_one relations. Get title from ID in the template Link to this post

    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

  • Nathan Cox
    Avatar
    Community Member
    99 Posts

    Re: has_one relations. Get title from ID in the template Link to this post

    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.

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: has_one relations. Get title from ID in the template Link to this post

    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

  • tipylis
    Avatar
    2 Posts

    Re: has_one relations. Get title from ID in the template Link to this post

    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.

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