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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

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

I am having trouble with utilizing a dataobject on more than one page


Go to End


2 Posts   1952 Views

Avatar
silversunhunter

Community Member, 6 Posts

23 March 2016 at 10:39am

Edited: 23/03/2016 10:41am

I have created a data object and can view the data on one page but I am having trouble understanding how to view it on multiple pages.

My data object:

class AgsMember extends DataObject {
     public static $db = array(
       //......removed for readability
    );

     // One-to-one relationship 
    public static $has_one = array(
        'ProfilePicture' => 'Image',
        'AgsMemberPage' => 'AgsMemberPage'
        //=====================================
        //should I be adding the other page here?        
    );

    // Summary fields
    public static $summary_fields = array(
    ///...............removed for readability
    );

    public function getCMSFields_forPopup() {
    //.....................
        );
    }
}

I can utilize the data on the AgsMemberPage
class AgsMemberPage extends Page{
    private static $has_many = array(
      'AgsMembers'=>'AgsMember'
    );
    public function getCMSFields()
    {
        $fields = parent::getCMSFields();
        $fields->addFieldToTab('Root.AgsMember', GridField::create(
            'Ags Member',
            'Ags Member List',
            $this->AgsMembers(),
            GridFieldConfig_RecordEditor::create()
        ));

        return $fields;
    }
}

class AgsMemberPage_Controller extends Page_Controller{
	public function init() {
        parent::init();

        }
}

But I can not utilize it on another page

class MemberDirectoryPage extends Page {
    private static $has_many = array(
      'AgsMembers'=>'AgsMember'
    );
    public function getCMSFields()
    {
        $fields = parent::getCMSFields();
        return $fields;
    }
}

class memberDirectoryPage_Controller extends Page_Controller{
	public function init() {
        parent::init();
    }
}

Avatar
ZarockNZ

Community Member, 17 Posts

20 September 2016 at 4:47pm

Edited: 20/09/2016 4:48pm

Hi,

The issue here is that the AgsMember class is being tied to one page type (AgsMemberPage), in the $has_one array 'AgsMemberPage' => 'AgsMemberPage' so cannot also belong to the MemberDirectoryPage. $has_many is just the other end of the $has_one relation, so 2 has_many's and 1 has_one does not match up.

I don't really understand why members need to be tied to a particular page type; personally I would create AgsMembers independent of any pages with its own Model Admin to manage them in the CMS. But the main part of the solution is in the second page type, if I understand what you want to do...

If you just want to output all of the members on the MemberDirectoryPage, say looping through them in the template and outputting a list on the screen, remove the $has_many and create a function called something like AgsMembers(), then in this function you can get all the AgsMembers in the database and return them to the template for output, no relation between the members and the directory page is required.


class MemberDirectoryPage extends Page {
    public function getCMSFields()
    {
        $fields = parent::getCMSFields();
        return $fields;
    }
}

class memberDirectoryPage_Controller extends Page_Controller{
	public function init() {
        parent::init();
    }

   public function AgsMembers() {
      return AgsMembers::get();   // Just get all members in the DB and return.
  }
}

Then in the template (.ss) for the MemberDirectory page...


<ul>
  <% loop $AgsMembers %>
    <li>$Name</li>
  <% end_loop %>
</ul>

If you did want to utilise the members on another page where, for example, only the members a CMS user/admin selects appear on the directory and not all. Then that is a slightly different solution involving the $many_many relation.

In this case the code for the AgsDirectory page would be as follows...


class MemberDirectoryPage extends Page {
  
  public static $many_many = array(
    'SelectedMembers' => 'AgsMember'
  ) ;

  public function getCMSFields()
    {
        $fields = parent::getCMSFields();

        $fields->addFieldToTab('Root.AgsMember', GridField::create(
            'SelectedMembers',
            'Selected Members',
            $this->SelectedMembers(),
            GridFieldConfig_RelationEditor::create()
        ));

        return $fields;
    }
}

class memberDirectoryPage_Controller extends Page_Controller{
	public function init() {
        parent::init();
    }
}

And the template code for the directory page...


<ul>
  <% loop $SelectedMembers %>
    <li>$Name</li>
  <% end_loop %>
</ul>

Regards,
DouG