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 /

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


Reply


4 Posts   287 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, 1097 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, 1097 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