Jump to:

22976 Posts in 11702 Topics by 2826 members

General Questions

SilverStripe Forums » General Questions » Many to Many relationship issue

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: 661 Views
  • Lazarus404
    Avatar
    Community Member
    71 Posts

    Many to Many relationship issue Link to this post

    Hey guys,

    So, I've modified an existing SilverStripe site and I'm now suffering a confusing error. Originally, the site would list projects that belonged to the members of the site. These projects were in a one to many relationship, so clients could have multiple projects. However, it now transpires that these projects may have multiple members. Ergo, I had to change the has_one relationship for project members to a has_many relationship. The member object was already being extended using a decorator which looks like this :-

    class MemberDecorator extends DataObjectDecorator
    {
       function extraStatics() {
          return array(
             'db'   =>   array(
                'CompanyName'   =>   'Varchar'
             ),
             'has_many'   => array(
                'Projects'   =>   'Project'
             )
          );
       }
       
       public function updateCMSFields(FieldSet $fields) {
          $fields->addFieldToTab('Root.Members', new CheckboxField('RedirectToProjects', 'Redirect to Projects on Login'));
       }
    }

    As you can see, the decorator adds the reciprocal has_many relationship for Projects.

    Now, when I try to run the application, I get the error "[User Error] Couldn't run query: SELECT ... FROM "Project" WHERE ("ParentID" = '5') Unknown column 'ParentID' in 'where clause'".

    What am I doing wrong? All other posts I've seen with this error are due to a missing "has_one" in the child object. I've not seen a many to many example, though both objects have a relationship notifier in the static "has_many" array. Should I be doing something else to enforce this?

    Thanks,
    Lee

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Many to Many relationship issue Link to this post

    A Many-Many is not two dataobjects with has_many relationships, instead they are many_many and belongs_many_many.
    For an example check the docs...

    http://doc.silverstripe.org/sapphire/en/topics/datamodel

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