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.


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

9 Posts   3762 Views


25 October 2007 at 10:11am (Last edited: 25 October 2007 10:15am), 4085 Posts

I'm building a site for a law firm, and they have a staff page, which uses my two custom modules, and 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 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.


25 October 2007 at 4:11pm Forum Moderator, 921 Posts

Hi there,

Running through this tutorial should provide some insight.



27 October 2007 at 6:38am (Last edited: 27 October 2007 6:39am), 4085 Posts

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?


27 October 2007 at 3:21pm (Last edited: 27 October 2007 3:21pm), Forum Moderator, 921 Posts

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(
    'FirstName' => 'FirstName',
    'Lastname' => 'Family Name',
    'Nationality' => 'Nationality'
$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!



25 November 2007 at 3:27am Community Member, 787 Posts

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 ?


5 December 2007 at 2:43am Community Member, 14 Posts

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!


5 December 2007 at 9:45pm Community Member, 787 Posts

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 !


6 December 2007 at 12:53am Community Member, 14 Posts

That worked, thanks a lot!

Go to Top