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.

Data Model Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

$has_many -> $has_many -> $belongs_many_many


Go to End


2 Posts   3118 Views

Avatar
Martijn

Community Member, 271 Posts

27 August 2009 at 8:59am

Edited: 27/08/2009 9:04am

I have one PageType CompanyPage which has many Companies and many Regions.
Each Company can be active in many Regions.

When I create a company, I want to assign several regions to it.
Like this, but in the Company editscreen:
http://doc.silverstripe.com/lib/exe/fetch.php?w=&h=&cache=cache&media=tutorial:gsoc-mentor-student-selection.png

So I need multiple CTF's but can't figure it out wich relations I need and which CTF's I need to manage them.
I can add Companies and Regions to the CompanyPage, but I can't assign Regions to a Company when editting a Company.

This are the realtions that I now have

class CompanyPage extends Page {
	
	public static $db = array(
	);
	
	static $has_many = array (
		'Companies' => 'Company',
		'Regions' => 'Region'

	);

}

class Company extends DataObject {
       static $db = array (
		'Name' => 'Varchar'
       );
      static $has_one = array (
		'Logo' => 'Image',
		'CompanyPage' => 'CompanyPage'

	);
      static $many_many = array(
         'Regions' => 'Region'
      );
}

class Region extends DataObject {

	static $db = array (
		'Name' => 'Varchar'
	);


	static $has_one = array(
			'CompanyPage' => 'CompanyPage'
   );
	 
	 static $belongs_many_many = array(
	 	'Companies' => 'Company'
	 );

}
 

On the CompanyPage I have 2 HasManyComplexTableFields for showing the Companies and Regions in a seperate tab. This works as expected.

I can add Regions. No problem.

Now when I add a ManyManyComplexTableField to the Company class I get this error:

Fatal error: Call to a member function Regions() on a non-object in D:\xampp\xampp\htdocs\silver_snippets\sapphire\forms\HasManyComplexTableField.php on line 91

When I add a HasManyComplexTableField I get:
Can't find a has_one relationship from 'Region' to 'Company'

So when I add a $has_one to the Region class, I can choose a Company on creating a new Regio. But it doesn't save the Company to the Region..

Can someone point me to the right direction to relate this 3 classes and how to administer them?

Avatar
Bambii7

Community Member, 254 Posts

8 September 2009 at 4:09pm

I'm no guru, just fumbling my way through silverstripe. I had this issue the other day with a manymanycomplextablefield. I found by removing the relationship for the blongs_many_many worked and saved. I was getting the exact same thing. Try removing the companies relationship from region.