Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Duplicate page should duplicate and link dataobjects

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

Page: 1 2
Go to End
Author Topic: 3173 Views
  • Nobrainer Web
    Avatar
    Community Member
    134 Posts

    Re: Duplicate page should duplicate and link dataobjects Link to this post

    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?

  • max5k
    Avatar
    Community Member
    8 Posts

    Re: Duplicate page should duplicate and link dataobjects Link to this post

    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.

  • Nobrainer Web
    Avatar
    Community Member
    134 Posts

    Re: Duplicate page should duplicate and link dataobjects Link to this post

    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.
             

  • Nobrainer Web
    Avatar
    Community Member
    134 Posts

    Re: Duplicate page should duplicate and link dataobjects Link to this post

    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/

  • ambient
    Avatar
    Community Member
    123 Posts

    Re: Duplicate page should duplicate and link dataobjects Link to this post

    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;
          }
       
          }

    ?>

  • Nobrainer Web
    Avatar
    Community Member
    134 Posts

    Re: Duplicate page should duplicate and link dataobjects Link to this post

    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

    3173 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.