Jump to:

3429 Posts in 1057 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » DataModel restrict children of children in a has_one/has_many/has_many relationship?

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

Page: 1
Go to End
Author Topic: 253 Views
  • mattstabeler
    Avatar
    Community Member
    2 Posts

    DataModel restrict children of children in a has_one/has_many/has_many relationship? Link to this post

    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?

  • martimiz
    Avatar
    Forum Moderator
    1067 Posts

    Re: DataModel restrict children of children in a has_one/has_many/has_many relationship? Link to this post

    From within the Job you should be able to:

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

  • mattstabeler
    Avatar
    Community Member
    2 Posts

    Re: DataModel restrict children of children in a has_one/has_many/has_many relationship? Link to this post

    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
  • martimiz
    Avatar
    Forum Moderator
    1067 Posts

    Re: DataModel restrict children of children in a has_one/has_many/has_many relationship? Link to this post

    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

    253 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.