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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

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

Many to Many relationship issue


Go to End


2 Posts   1182 Views

Avatar
Lazarus404

Community Member, 72 Posts

26 September 2011 at 2:45am

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

Avatar
swaiba

Forum Moderator, 1899 Posts

26 September 2011 at 7:09am

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