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

Unable to reproduce template public method in ORM


Go to End


2 Posts   756 Views

Avatar
fingermonster

Community Member, 1 Post

23 January 2017 at 11:40pm

Hello all, I have an issue that's probably very simple to solve, but has been driving me mad for weeks.

I have a Staff section with the following elements:

StaffProfile (Page - displays individual staff member details)
StaffHolder (Page - list all the staff profiles)
StaffDepartment (Dataobject - staff member departments, functioning as DataObject as page)

I call the method StaffMembers in the various templates to loop the StaffProfiles and this works as I want.
Called on StaffHolder I get all profiles, called on StaffDepartment (StaffHolder_show.ss) I get the profile from that Department.

However, I'm stretching the limits of template-based filtering/sorting and want to manipulate the data in the ORM.
No matter what I've tried, I can't re-produce what I'm able to do template-side.

I.e. if I create a simple function to get StaffMembers on StaffHolder, it will work on the holder page but the StaffMembers will not filter by their departments on StaffDepartment/StaffHolder_show.ss.

If I put the function on StaffDepartment, it does filter staff members on StaffHolder_show.ss. Only if I loop StaffDepartments on Staffholder, then StaffMembers do I get all the departments with their staff, which I don't want - as I don't want them broken down into departments on this page, and it retrieves duplicates where staff have multiple departments. I'm using the DataObjectAsPage module, but got the same results when I coded the page functionality from scratch from UncleCheese's video.

Any pointers on where I'm going wrong would be greatly appreciated.

Class/Controller Structure below:

StaffHolder.php

class StaffHolder extends DataObjectAsPageHolder {

	private static $allowed_children = array (
		'StaffProfile',
	);

	private static $has_many = array (
        'Departments'		=> 'StaffDepartment',
        'StaffMembers'	=> 'StaffProfile'
    );

    private static $has_one = array (
		'StaffPlaceHolder'	=> 'Image'
	);
        ...........
}

class StaffHolder_Controller extends DataObjectAsPageHolder_Controller {
	public static $item_class = 'StaffDepartment';

        ...........
}

StaffProfile.php

class StaffProfile extends Page {

    private static $belongs_many_many = array (
        'Departments'			=> 'StaffDepartment',
    );
    // I have tried the reverse relationship to StaffProfile and produces same results
........
}

StaffDepartment.php

class StaffDepartment extends DataObjectAsPage {

	public static $listing_page_class = 'StaffHolder';

	public static $item_sort = 'Title ASC';

	public static $plural_name = 'Departments';

    public static $singular_name = 'Department';

    private static $has_one = array (
        'StaffHolder' => 'StaffHolder'
    );

    private static $belongs_many = array (
        'StaffHolder' => 'StaffHolder'
    );

    private static $many_many = array (
        'StaffMembers' => 'StaffProfile'
    );
   .........

Avatar
martimiz

Forum Moderator, 1391 Posts

6 February 2017 at 6:37am

I'm not sure what the actual problem is, but the bit here, where you're defining the same relation twice, doesn't seem right to me:

    private static $has_one = array (
        'StaffHolder' => 'StaffHolder'
    );

    private static $belongs_many = array (
        'StaffHolder' => 'StaffHolder'
    );