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.

General Questions /

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

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

How to get the name corresponding to ID from other table?


Go to End
Reply


4 Posts   1020 Views

Avatar
esakrielaart

Community Member, 58 Posts

20 August 2011 at 8:04am

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

Avatar
swaiba

Forum Moderator, 1826 Posts

20 August 2011 at 8:44am

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

Avatar
esakrielaart

Community Member, 58 Posts

20 August 2011 at 10:11pm

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

Avatar
Ryan M.

Community Member, 309 Posts

21 August 2011 at 7:58am

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