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?
This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.
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.
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?
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.
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.
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/
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;
}
}
?>
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