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.

Archive /

Our old forums are still available as a read-only archive.

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

One-to-Many Relationship


Go to End
Reply


9 Posts   3783 Views

Avatar
UncleCheese

4086 Posts

25 October 2007 at 10:11am

Edited: 25/10/2007 10:15am

I'm building a site for a law firm, and they have a staff page, which uses my two custom modules, AttorneyHolder.ss and AttorneyPage.ss... Works great.

There is also a practice areas section that is just a straight page with an assortment of child pages for each practice area. The problem is that each attorney has many "practice areas" that need to be ticked off with checkboxes. Since the practice areas section is all plain pages, I'm assuming they don't have a special table in the DB, so this could get hairy.

How can I create a tab in my attorney module where the user can tick checkboxes for a dynamically generated list of all the practice areas they have added to their site? If I need to set up a special PracticeAreasPage.ss then I'm willing to do that, but since it's just straight text, I didn't see the point initially.

Do I need to use the HasManyComplexTableField? If so, a push in the right direction would be REALLY helpful. Thanks a lot.

Avatar
Sean

Forum Moderator, 922 Posts

25 October 2007 at 4:11pm

Hi there,

http://doc.silverstripe.com/doku.php?id=tutorial:5-dataobject-relationship-management

Running through this tutorial should provide some insight.

Cheers,
Sean

Avatar
UncleCheese

4086 Posts

27 October 2007 at 6:38am

Edited: 27/10/2007 6:39am

Wow, that's great. Thanks a lot. Is there any way I can filter my results, though? The problem I'm having is that I want each AttorneyPage to have its own set of schools. Right now, when I add a new school to my HasManyComplexTableField, it's a global change, available to every attorneypage.

I notice that the ComplexTableField supports a $sourceFilter variable. That seems to be what I need, e.g., $sourceFilter = "'AttorneyPageID = '$this->ID'"...

Why isn't that available to the HasManyComplexTableField?

Avatar
Sean

Forum Moderator, 922 Posts

27 October 2007 at 3:21pm

Edited: 27/10/2007 3:21pm

Rather than filtering on the table itself, you can filter the actual source items. There is a method on the DataObject class:

public function getComponents($componentName, $filter = "", $sort = "", $join = "", $limit = "", $having = "")

This gets has_many components on an object.

For example, if you have:

class CoolPage extends Page {

static $has_many = array(
'Students' => 'Student'
);

// Custom 'getter' for Students, we can specify filters, sorts and limits here on getComponents()
function StudentsFiltered() {
return $this->getComponents('Students', "ParentID = $this->ID");
}

function getCMSFields() {
$fields = parent::getCMSFields();
$tablefield = new HasManyComplexTableField(
$this,
'StudentsFiltered',
'Student',
array(
    'FirstName' => 'FirstName',
    'Lastname' => 'Family Name',
    'Nationality' => 'Nationality'
),
'getCMSFields_forPopup'
);
$tablefield->setAddTitle( 'A Student' );

$fields->addFieldToTab( 'Root.Content.Students', $tablefield );

return $fields;
}

}

I believe this is how you could do it... using getComponents() we can get the has_many components and manipulate them in certain ways - filter, sort and limit etc. There's also getManyManyComponents() if you're doing a many_many relationship. These functions can be found on the DataObject class.

Hope this helps!

Cheers,
Sean

Avatar
Fuzz10

Community Member, 787 Posts

25 November 2007 at 3:27am

I'm currently in need of the same functionality, but I can't get the filter to work. What I don't understand in the example above is how and where the filter is called ?

Avatar
Hubertus

Community Member, 14 Posts

5 December 2007 at 2:43am

I'm in the same boat I guess; can't get this filter to work.

I have tons of related objects in a 1-many relation, and I would like my HasManyComplexTableField to show only those that are associated to my current page. The table outputs all objects, and while they can't be selected, they clutter up the table.

Could anybody shed some light onto where to put this code? Many thanks in advance!

Avatar
Fuzz10

Community Member, 787 Posts

5 December 2007 at 9:45pm

I could not get this working so I ended up using the $sourcefilter param of ComplexTableField.
Presumably , that is not the correct method , but it's an easy fix that works.

Good luck !

Avatar
Hubertus

Community Member, 14 Posts

6 December 2007 at 12:53am

That worked, thanks a lot!

Go to Top