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.

Data Model Questions /

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

Getting fields from has_one ID through has_many

Go to End

2 Posts   2027 Views


Community Member, 16 Posts

23 June 2011 at 2:06pm

I've got a fairly complex set of data (at least for me) and I can't figure out how to get point A to point D. I want to get from:
PageObject has_one DataObjectA has_many DataObjectsB has_one DataObjectC. I want fields form DataObjectC to be available on my PageObject, and I can't figure out how to get anything other than the ID.
Here are the Page and DataObjects, all managed through Uncle Cheese's very excellent remodelled ModelAdmin (and here with irrelevant fields removed for brevity):

class LegislativeSession extends DataObject {
   static $many_many = array( 'Legislators' => 'Legislator');
   static $has_many = array('Bills' => 'Bill');
   static $has_one = array('

class Legislator extends DataObject {
   static $db = array('Name'=>'Text');

   static $has_many = array(

   static $belongs_many_many = array('LegislativeSessions'=>'LegislativeSession');

class Bill extends DataObject {
   static $has_one = array(
       'LegislativeSession' => 'LegislativeSession');

   static $has_many = array('Votes'=>'Vote');
   public function getMyVotes(){
		return $this->Votes();

class Vote extends DataObject {
   static $has_one = array(

class Scorecard extends Page {
	static $has_one = array('LegislativeSession'=>'LegislativeSession');
	public function getBills(){
		$Bills = Dataobject::get("Bill", "Bill.LegislativeSessionID = $legissessionid", "Bill.Title ASC", "LEFT JOIN LegislativeSession ON LegislativeSession.ID = Bill.LegislativeSessionID", "");
		return $Bills;
class Scorecard_Controller extends Page_Controller {
  static $allowed_actions = array( 

   public function getIndividualBill(){ 
      if ($this->request->latestParam('Action')) { 
         $parameter = $this->request->latestParam('ID'); 
         if ($parameter) { 
            $BillID = Convert::raw2xml($parameter); 
               $BillInfo = DataObject::get_by_id('Bill', $BillID); 
               return $BillInfo; 

..and then this template:

<section class="primary">
    <% if IndividualBill %>
        <% control IndividualBill %>
        <% control MyVotes %>               
            <% end_control %>
    <% else %>
    <% end_if %>
<section class="secondary">
    <% control getBills %>
        <li><a href="{$Top.Link}retrieve_bill/$ID" class="$LinkingMode">$Title</a></li>
    <% end_control %>

I want to list Legislator Name on the Scorecard Page, and I tried adding this to the Bill object:
public function Voter() {

and calling

<% control Voter %>
<% end_control %>

..but that doesn't return anything. Help, please?


Community Member, 545 Posts

28 June 2011 at 9:08am

Silverstripe should take care of your relationships for you. You just need to call the <% control Legislator %> instead of $LegislatorID, that way you'll have full access to the entire dataobject.

The other option is use a function like this on Vote

function getLegislatorName() {
return "Legislator.Name";

That way $LegislatorName becomes a property on the Vote object

Look into the dot notation stuff and get property calls, you can do a lot of cool stuff. I just figured it out a few months ago and love it