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

Duplicate page should duplicate and link dataobjects


Go to End


14 Posts   7875 Views

Avatar
Nobrainer Web

Community Member, 138 Posts

6 March 2012 at 9:25pm

So for instance if i have

public static $has_many = array(
'Images' => 'Image',
'Employees' => 'DataObject'
);

then i should use

$items_to_duplicate = array(
'Images',
'Employees' //anything you want to duplicate
);

Or?

Avatar
mgherkins

Community Member, 8 Posts

7 March 2012 at 10:59am

yes, exactly. it just checks for the key of the relationship.

glad, if that works for you.

be careful in any production environment, though.
as this is not too much tested yet :)

i thought something like that would also be a nice core feature,
so that you could have

public static $relations_to_copy = array(
  'myImages',
  'myGeoObjects'
);

on any Page ?

..just my 2 cents.

Avatar
Nobrainer Web

Community Member, 138 Posts

14 June 2012 at 10:33pm

Hi guys,

I'm doing a multilingual website in SS and i now i face a similar issue, when making a translation of a page, my related dataobjects are not added to the translation.

Is it possible to achive the same functionality as in earlier posts, defining a list of relations that must also be duplicated to the translation?
This could maybe be done when the first translation happens, or maybe as a check On Before Write on the page?

Thinking something like

On Before Write (Perhaps only if page has no id yet)
If page is a translation,
if translation parent has dataObjects
Duplicate dataObjects to translation
// Perhaps dataobject should have a checkbox to decide if it should be active, and setting to null if dataObject is being duplicated.

Avatar
Nobrainer Web

Community Member, 138 Posts

14 June 2012 at 11:47pm

To my above question,

I found a great log from Balbus design that does exactly what i need, duplicates the dataobjects from the parent translation page.
See the guide here: http://www.balbuss.com/translatable-duplicate-linked-dataobjects/

Avatar
ambient

Community Member, 130 Posts

12 July 2012 at 2:30am

Hi Guys,
I'm having trouble getting this to work when duplicating a page with dataobjects.
I not great at programming, have I made the correct changes to the code below?

class Event_Controller extends Page_Controller {
	public function duplicate() {

		$items_to_duplicate = array( 
		'Date',
		'Name',
		'Location',
		'Thumbnail'//anything you want to duplicate 
		);
		
		$page = parent::duplicate();
		
		//duplicate has many items 
		foreach ($this->has_many() as $key => $className) { 
		if (in_array($key, $items_to_duplicate)) { 
		foreach ($this->{$key}() as $item) { 
		$newField = $item->duplicate(); 
		$id = get_class($this) . 'MyEventMemberID'; 
		$newField->{$id} = $page->MyEventMemberID; 
		$newField->write(); 
		} 
		} 
		}
		
		//duplicate many_many items 
		/*foreach ($this->many_many() as $key => $className) { 
		if (in_array($key, $items_to_duplicate)) { 
		$page->{$key}()->addMany($this->{$key}()->getIdList()); 
		} 
		}*/
		
		return $page; 
		}
	
		}



?>

Avatar
Nobrainer Web

Community Member, 138 Posts

12 July 2012 at 7:43am

Hi ambient,

The duplicate function should go into you page class (or rather the class that has the relation) and not the controller.

The items to duplicate should be defined as the relation name to the dataobjects to duplicate.
So if you defined the relation like public static $has_many = array('PageImages' => 'PageImage'); you should have:
$items_to_duplicate = array( 'PageImages' );

Hope that it helps

Go to Top