<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>has_many | Nerdpress.org</title>
	<atom:link href="https://nerdpress.org/tag/has-many/feed/" rel="self" type="application/rss+xml" />
	<link>https://nerdpress.org</link>
	<description>...dev, tech problems and solutions.</description>
	<lastBuildDate>Sun, 11 Mar 2012 10:17:31 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>Silverstripe &#8211; has_many- and many_many-relations when duplicating pages</title>
		<link>https://nerdpress.org/2012/03/07/silverstripe-has-many-and-many-many-relations-when-duplicating-pages/</link>
					<comments>https://nerdpress.org/2012/03/07/silverstripe-has-many-and-many-many-relations-when-duplicating-pages/#comments</comments>
		
		<dc:creator><![CDATA[Max Girkens]]></dc:creator>
		<pubDate>Wed, 07 Mar 2012 03:48:03 +0000</pubDate>
				<category><![CDATA[CMS]]></category>
		<category><![CDATA[Silverstripe]]></category>
		<category><![CDATA[duplicate pages]]></category>
		<category><![CDATA[has_many]]></category>
		<category><![CDATA[many_many]]></category>
		<guid isPermaLink="false">https://nerdpress.org/?p=2087</guid>

					<description><![CDATA[<p>When you duplicate a page in the Silverstripe Sitetree, related &#8220;has_one&#8221; Items are automatically copied, which is nice. But if you have, lets say, multiple related images on that Page, those are not copied when you duplicate that Page. After reading this discussion, i found out that it&#8217;s actually pretty easy to &#8220;fix&#8221; this. This &#8230; </p>
<p class="link-more"><a href="https://nerdpress.org/2012/03/07/silverstripe-has-many-and-many-many-relations-when-duplicating-pages/" class="more-link">Continue reading<span class="screen-reader-text"> "Silverstripe &#8211; has_many- and many_many-relations when duplicating pages"</span></a></p>
The post <a href="https://nerdpress.org/2012/03/07/silverstripe-has-many-and-many-many-relations-when-duplicating-pages/">Silverstripe – has_many- and many_many-relations when duplicating pages</a> first appeared on <a href="https://nerdpress.org">Nerdpress.org</a>.]]></description>
										<content:encoded><![CDATA[<p>When you duplicate a page in the <a href="http://www.silverstripe.org/">Silverstripe</a> Sitetree, related &#8220;has_one&#8221; Items are automatically copied, which is nice.</p>
<p>But if you have, lets say, multiple related images on that Page, those are not copied when you duplicate that Page.<span id="more-2087"></span></p>
<p>After reading <a href="http://www.silverstripe.org/data-model-questions/show/17342?start=8">this discussion</a>, i found out that it&#8217;s actually pretty easy to &#8220;fix&#8221; this.</p>
<p>This what i ended up with and seems to be working fine:</p>
<pre class="brush: php; title: ; notranslate">
//assuming your page has the following related Dataobjects

public static $has_many = array(
'Images' =&gt; 'MyImageDataObject'
);

public static $many_many = array(
'Something' =&gt; 'AnotherPageTypeOrWhatever

);
</pre>
<p>The following code would automatically duplicate the has_many related DataObjects<br />
and just set the relation to the many_many Objects on the created page:</p>
<pre><script src="https://gist.github.com/2014023.js?file=silverstripe_duplicate_relations.php"></script><noscript><pre><code class="language-php php">&lt;?php 

public function duplicate() {
 
  $items_to_duplicate = array(
    &#039;Images&#039;,
    &#039;Somehting&#039;
  );
 
  $page = parent::duplicate();
 
  //duplicate has many items
  foreach ($this-&gt;has_many() as $key =&gt; $className) {
    if (in_array($key, $items_to_duplicate)) {
      foreach ($this-&gt;{$key}() as $item) {
        $newField = $item-&gt;duplicate();
        $id = get_class($this) . &#039;ID&#039;;
        $newField-&gt;{$id} = $page-&gt;ID;
        $newField-&gt;write();
      }
    }
  }
 
  //set the relation to many_many items on created page
  foreach ($this-&gt;many_many() as $key =&gt; $className) {
    if (in_array($key, $items_to_duplicate)) {
     $page-&gt;{$key}()-&gt;addMany($this-&gt;{$key}()-&gt;getIdList());
    }
  }
 
  return $page;
}</code></pre></noscript></pre>
<p>..as already suggested <a href="http://www.silverstripe.org/data-model-questions/show/17342?start=8">in the forum</a>, i still think this would make a handy core feature, wouldn&#8217;t it?<br />
Would be cool to globally specify relations to duplicate in the page-config with something like</p>
<pre class="brush: php; title: ; notranslate">
public static $relations_to_copy = array(
 'Images',
 'Somehting'
);
</pre>The post <a href="https://nerdpress.org/2012/03/07/silverstripe-has-many-and-many-many-relations-when-duplicating-pages/">Silverstripe – has_many- and many_many-relations when duplicating pages</a> first appeared on <a href="https://nerdpress.org">Nerdpress.org</a>.]]></content:encoded>
					
					<wfw:commentRss>https://nerdpress.org/2012/03/07/silverstripe-has-many-and-many-many-relations-when-duplicating-pages/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
