Jump to:

23451 Posts in 18899 Topics by 2877 members

General Questions

SilverStripe Forums » General Questions » Setting up the correct relation

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 393 Views
  • woodb2
    Avatar
    Community Member
    48 Posts

    Setting up the correct relation Link to this post

    I have a basic site for Project tracking. Originally we were just keeping track of what Projects the Developers are working on.

    I had a many_many relationship between two DataObjects Project and Developer

    Project extends DataObject {

       static $many_many = array(
       'Developers' => 'Developer'
       );

    Developer extends DataObject {

       static $belongs_many_many = array(
       'Projects' => 'Project'
       );

    I'm using ModelAdmin (we're just looking to manage it in the backend).

    We would like to assign a percentage of the Developer's time when we assign them to a project(s). I haven't been able to come up with a good method to add the PercentTime field into this. Originally I was going to use a checkboxfieldset to pick the developers, but you can't assign the percent of the developers time then. The more I thought about it, there didn't seem to be a good way to enter data on two many_many fields that are related to each other.

    Brian

  • Bambii7
    Avatar
    Community Member
    254 Posts

    Re: Setting up the correct relation Link to this post

    Make a third object for TimeEntry or what have you

    Time extends DataObject {

    $db = array(
    'time' => 'Int'
    );

    $has_one = array(
    'dev' => 'Developer',
    'pro' => 'Project'
    );

    }

    then add a has_many -> time to projects

    $has_many = array(
    'times' =>'Time'
    )

    You should then be able to assign time in admin.

  • woodb2
    Avatar
    Community Member
    48 Posts

    Re: Setting up the correct relation Link to this post

    Thanks, that does allow me to assign the time, but I have an odd issue now. I'm adding the Developer and Time with a DataObjectManger (I get the same results with a ComplexTableField) like so:

    Project extends DataObject

          //Add Developer and Percent of Time
          $manager = new DataObjectManager(
             $this,
             'Developers',
             'Developer'
          );
          $fields->addFieldToTab("Root.Resources", $manager);

    The Resource Tab that is created Let's me add the Developer and Time, but when I create a new Project, the existing Developer and Time are already entered. I was expecting to see a blank table every time I create a new project

    If I change Project to be a Page instead of a DataObject, it works correctly. Reading the documentation on DataObjectManager (and ComplexTableField) the $this states that it's the current Controller. Do I have to use a Page class in order to make this work or is there another way to enter the Developer and Time using a DataObject?

    Thanks

  • Bambii7
    Avatar
    Community Member
    254 Posts

    Re: Setting up the correct relation Link to this post

    Ops sorry I think you'll want many_many not has_many which is what I first said.

    Project
    $many_many = array(
    'times' =>'Time'
    )

    Time
    $belongs_many_many
    'project' = 'project'

    393 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.