Jump to:

23455 Posts in 18904 Topics by 2877 members

General Questions

SilverStripe Forums » General Questions » How to get the name corresponding to ID from other table?

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 875 Views
  • esakrielaart
    Avatar
    Community Member
    56 Posts

    How to get the name corresponding to ID from other table? Link to this post

    Hello,

    I have a table which contains only ID's from other tables and some additional data. Now I read this table, and I find a record, for instance

    1, 10, 13
    2, 15, 26
    .
    .
    .

    Now this is found using DataObject::get('CallerName'); inside the SomePage_Controller class

    Offcourse I want to display in my templatefile not these rows, but the corresponding names which are stored in other tables (for instance Member, Articles, ....). How can I achieve this? just can't solve this at the moment.

    Thanks for you help,
    Maurice

  • swaiba
    Avatar
    Forum Moderator
    1788 Posts

    Re: How to get the name corresponding to ID from other table? Link to this post

    Hi Maurice,

    something like this...

    templatefile.ss

    <% control CallerName %>

    has_one member -
    $Member.Firstname

    has_many Articles -
    <% control Articles %>
    $Name
    <% end control %>

    <% end control %>

    more information in the tutorials and in the documentation about templates.

    Barry

  • esakrielaart
    Avatar
    Community Member
    56 Posts

    Re: How to get the name corresponding to ID from other table? Link to this post

    Thanks for your help, however I still do not get it working. Maybe I am referencing wrong. I will give some more details of the situation at hand:

    Profile.php

    class Profile extends Page {
    }

    class Profile_Controller extends Page_Controller {
    function CurrentSubscriptions() {
       // s11s = s ubscription s
       $cMember = Member::currentUserID();
       $s11s = DataObject::get("DanceCouples","PartnerAID = {$cMember} OR PartnerBID = {$cMember}");
       return $s11s;

    }
    }

    Than I have the following code that creates the Model for the couples of persons:

    class DanceCouples extends DataObject {
    static $db = array(
       'DansNiveau' => "Int"
    );

    // Een lid kan meerdere koppels vormen
    static $has_one = array(
       'PartnerA' => 'DanceMemberInfo',
       'PartnerB' => 'DanceMemberInfo'
    );

    And the following Model for 'DanceMemberInfo':

    class DanceMemberInfo extends Member {
    static $db = array(
       'Voorletters' => 'Varchar',
       'Geslacht' => "Enum('m,v')",
       'Adres' => 'Varchar',
       'Postcode' => 'Varchar',
       'Plaats' => 'Varchar',
       'Telefoon' => 'Varchar',
       'XtraCardNr' => 'Varchar'
    );

    // Meerdere dansniveaus per lid toegestaan
    static $has_many = array(
       'DanceCouples' => 'DanceCouples',
    );
    }

    The IDs which 'DansNiveau' refers to are created here:

    class DanceCourses extends DataObject {
    static $db = array(
       'Name' => 'Varchar'
    );
    }

    Is this a proper way of creating a model, so I can read all data to the template? How can I reference to it? I tried using the advanced-template reference of SS, but it did not help for me. Could you maybe give some suggestions?

    Thanks a lot!
    Maurice

  • Ryan M.
    Avatar
    Community Member
    309 Posts

    Re: How to get the name corresponding to ID from other table? Link to this post

    I think you forgot to add a has_one or has_many relationship from DanceCouples to DanceCourses.

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