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_many relations when translating a page


Go to End


7 Posts   1628 Views

Avatar
Felicitas

Community Member, 16 Posts

1 June 2012 at 11:46pm

Edited: 01/06/2012 11:48pm

When I translate a page the many-many relations from the original page are not copied to the translated page. I have to select the related items again on the translated page.

Is there a way to automate this?

I was looking at the code on this website (concerning relationships when duplicating a page), but I don't know if the same is possible when translating a page?

Could someone start me off in the right direction (I use Silverstripe 2.4.7)?

Avatar
Felicitas

Community Member, 16 Posts

2 June 2012 at 2:08am

Edited: 02/06/2012 2:09am

Same question here and here ...
But no solution?

Avatar
swaiba

Forum Moderator, 1899 Posts

2 June 2012 at 4:34am

The second link from max5k seems to give a solution...

Avatar
Felicitas

Community Member, 16 Posts

2 June 2012 at 5:34pm

Edited: 02/06/2012 5:37pm

Yes, but the solution of max5k can be used when duplicating pages. But I don't know how and where to implement his code when translating pages? In Translatable.php?

Avatar
Felicitas

Community Member, 16 Posts

2 June 2012 at 11:55pm

Edited: 10/06/2012 3:06am

It works.

Added the following code at the end of the function createTranslation($locale) in Translatable.php

foreach( $originalPage->many_many() as $key => $className ){ 
$newTranslation->{$key}()->addMany($originalPage->{$key}()->getIdList()); 
}

The many-many relations from the original page are now copied to the translated page.

In combination with the TranslatableDataObject from Uncle Cheese this works great for me!

Avatar
yorkie

Community Member, 2 Posts

13 June 2012 at 7:56pm

I had a similar problem and I solved it by adding an onAfterWrite method to my object and using Felicitas little code snippet. This way I don't have to worry about touching other code. I am not happy with the way I get the original page as it is a hack to have to use the language locale in the code.

WARNING: I am still testing the code, seems to work well though

public function onAfterWrite()
{
	parent::onAfterWrite();
       
        //Todo, there must be a better way to get the original page
	$originalPage = $this->getTranslation('de_DE');

	if($this->ID == $originalPage->ID) return;

	foreach ($originalPage->many_many() as $key => $className)
	{
		$this->{$key}()->addMany($originalPage->{$key}()->getIdList());
	}
}

Avatar
yorkie

Community Member, 2 Posts

13 June 2012 at 8:09pm

A quick correction, the code should be:

public function onAfterWrite()
{
	parent::onAfterWrite();
       
        //Todo, there must be a better way to get the original page
	$originalPage = $this->getTranslation('de_DE');

	if($!$originalPage) return;
        

	foreach ($originalPage->many_many() as $key => $className)
	{
		$this->{$key}()->addMany($originalPage->{$key}()->getIdList());
	}
}

Sorry