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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

General Questions /

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

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

Setting up the correct relation


Go to End


4 Posts   634 Views

Avatar
woodb2

Community Member, 48 Posts

12 June 2012 at 7:01am

Edited: 12/06/2012 11:08am

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

Avatar
Bambii7

Community Member, 254 Posts

13 June 2012 at 1:28pm

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.

Avatar
woodb2

Community Member, 48 Posts

14 June 2012 at 1:19am

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

Avatar
Bambii7

Community Member, 254 Posts

14 June 2012 at 3:40pm

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'