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

ModelAdmin - Inverse component Error


Go to End


8 Posts   1585 Views

Avatar
Andre

Community Member, 146 Posts

11 January 2015 at 10:17am

Hi there, I tried to extend the Member Object with a Friendship Relation.

Therefore the Member Object gets a many_many relation to itself.

Here is some code:

class BaseMember extends DataExtension {
    
    private static $many_many = array(
        'FriendsRequested' => 'Member'
    );
    
    private static $belongs_many_many = array(
        'FriendsAccepted' => 'Member.FriendsRequested'
    );
    
    public function Friends(){
        return $this->owner->FriendsRequested()->innerJoin("Member_FriendsRequested", "\"Member_FriendsRequested\".\"MemberID\" = \"FriendsAccepted\".\"ChildID\" AND \"Member_FriendsRequested\".\"ChildID\" = \"FriendsAccepted\".\"MemberID\"", "FriendsAccepted");
    }
}

Now I ran into the Problem, that a many_many relation to itself leads to a "Inverse component of Member.FriendsRequested not found (Member)" Error on ModelAdmin. Everywhere else this is working without any errors or Problems.

I allready tried to remove the FriendsRequested Tab from the CMS Fields (by using updateCMSFields), but without any success.

Does anyone of you have an idea, how to solve this, as I really need access to the ModelAdmin in the Backend again.

Avatar
Andre

Community Member, 146 Posts

11 January 2015 at 3:25pm

Edited: 11/01/2015 3:25pm

I tried to reverse engineer this error/bug and came across the problem, that in general the ModelAdmin results in an Inverse component Error when you have a multible many_many relations to the same object.

Here is some code which also fails:

class Category extends DataObject {
    
    private static $db = array(
        "Title" => "Varchar(255)"
    );
    
    private static $many_many = array(
        'BigProducts' => 'Product',
        'SmallProducts' => 'Product'
    );
}

class Product extends DataObject {
    
    private static $db = array(
        "Title" => "Varchar(255)"
    );
    
    private static $belongs_many_many = array(
        'BigCategories' => 'Category.BigProducts',
        'SmallCategories' => 'Category.SmallProducts'
    );
}

Avatar
looney

Community Member, 2 Posts

21 January 2015 at 6:52am

Hello Andre, I just had the same problem. The code with dot-notation worked for me perfectly fine in Silverstripe 3.1.6, but now I've updated to 3.1.9 it throws this error... it seems to me like something changed in the framework. But I can't figure out what or why and if it's a bug or a different approuch to things.

Did you manage to find a solution / the problem / a workaround?

Avatar
martimiz

Forum Moderator, 1391 Posts

22 January 2015 at 2:51am

I'm not sure if this helps, but here's a stackoverflow post addressing this issue with a custom getCMSFields() function...

Avatar
Andre

Community Member, 146 Posts

22 January 2015 at 2:59am

Hi Looney, hi martimiz,

customizing the getCMSFields Method doesn't help. I allready tried that. But I also have halfway good news.

I solved the Problem, but you need to change the core (and currently I didn't checked my Patch for possible errors, that are followups).

In /framework/model/DataObject.php you have to change the many_many method:


	public function many_many($component = null) {

                ...

                                // Try belongs_many_many
				$belongsManyMany = Config::inst()->get($class, 'belongs_many_many', Config::UNINHERITED);
				$candidate = (isset($belongsManyMany[$component])) ? $belongsManyMany[$component] : null;
				if($candidate) {
					
// Here are my changes
					// If $candidate is of dot notation for multiple many_many relations to the same class
					if(strpos($candidate,'.') !== FALSE){
						$candidate=explode('.', $candidate);
						$candidate=$candidate[0];
					}
                                        
					$childField = $candidate . "ID";

// end of my changes

					// We need to find the inverse component name
				       
                                        ....
		}
		
		return isset($items) ? $items : null;
	}

Avatar
Andre

Community Member, 146 Posts

22 January 2015 at 3:03am

I created a patch repository, that will overwrite some files within the silverstripe core using composer.

The following apptemplate can help you, how to use the fixes repository.

https://github.com/andrelohmann/silverstripe-apptemplate

It will be updated to Silverstripe 3.1.9 within the next two days (currently its on ss3.1.8).

Avatar
looney

Community Member, 2 Posts

22 January 2015 at 6:29am

Very Nice! Thanks a lot Andre! With your fix in DataObject it's working fine now! :)

Avatar
Andre

Community Member, 146 Posts

22 January 2015 at 7:38am

you're welcome

but keep in mind, I didn't checked it for side effects, even if I'm using SS heavily to build real complex applications and I didn't found any side effect so far.