Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » One-to-Many Relationship

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

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

Page: 1 2
Go to End
Author Topic: 3708 Views
  • UncleCheese
    Avatar
    4085 Posts

    One-to-Many Relationship Link to this post

    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.

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: One-to-Many Relationship Link to this post

    Hi there,

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

    Running through this tutorial should provide some insight.

    Cheers,
    Sean

  • UncleCheese
    Avatar
    4085 Posts

    Re: One-to-Many Relationship Link to this post

    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?

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: One-to-Many Relationship Link to this post

    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

  • Fuzz10
    Avatar
    Community Member
    787 Posts

    Re: One-to-Many Relationship Link to this post

    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 ?

  • Hubertus
    Avatar
    Community Member
    14 Posts

    Re: One-to-Many Relationship Link to this post

    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!

  • Fuzz10
    Avatar
    Community Member
    787 Posts

    Re: One-to-Many Relationship Link to this post

    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 !

  • Hubertus
    Avatar
    Community Member
    14 Posts

    Re: One-to-Many Relationship Link to this post

    That worked, thanks a lot!

    3708 Views
Page: 1 2
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.