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

DataModel restrict children of children in a has_one/has_many/has_many relationship?


Go to End


4 Posts   1227 Views

Avatar
mattstabeler

Community Member, 2 Posts

30 January 2014 at 2:59am

I want to restrict the list of objects provided when editing a data item with a one to one relationship to another object, who's parent has a one to many relationship to an object also with a one to one relationship with the original object....

Customer      has many       Jobs
Customer      has many       Sites

Job                 has one          Customer
Job                 has one          Site (this should be restricted to only those under Customer)

Site                 has one          Customer
Site                 has many       Jobs

class Customer extends DataObject {

    public static $has_many = array(
         'Sites'                    => 'Site',
         'Jobs'                    => 'Job'
    );
 
}


class Job extends DataObject {

    public static $has_one = array(
         'Site'                        => 'Site',
         'Customer'                    => 'Customer'
    );
 
}


class Site extends DataObject {

    public static $has_one = array(
         'Customer'                    => 'Customer'
    );

    public static $has_many = array(
         'Jobs'                    => 'Job'
    );

}

How do I make the list of Sites available to Job be restricted to only those Sites related to the Job's Customer?

Avatar
martimiz

Forum Moderator, 1391 Posts

30 January 2014 at 9:29am

From within the Job you should be able to:

$sites = $this->Customer()->Sites();

Avatar
mattstabeler

Community Member, 2 Posts

31 January 2014 at 12:53am

Thanks martimiz - I re-read my question and realised that I didn't really explain my problem fully.

I have extended ModelAdmin so that I can manage my customer Objects through the admin interface (actually I have inherited a half finished project that uses SilverStripe, and this is the first time I have used it, so when I say I, i mean the project i'm working on!).

When editing a job, I want to ensure that the list of available Sites (in the built in drop-down list) is restricted to only the Sites of the Customer that the Job is for, if that makes sense!

Attached Files
Avatar
martimiz

Forum Moderator, 1391 Posts

31 January 2014 at 2:48am

Ah I see... On creation of a new Job the customer isn't set yet, so at that point you'd want.to use javascript to update the selection when the customer changes...

I've never used it, but maybe something like this module could work? It isn't really documented, but it's not a lot of code either...

https://github.com/sheadawson/silverstripe-dependentdropdownfield