<p>Since 3.4, you do this by passing an array of site ID/site-enabled mappings like so:</p>
<pre class="lang-php prettyprint-override"><code>$entry-&gt;enabledForSite = [1 =&gt; true, 2 =&gt; true];
</code></pre>
<p>Where <code>1</code> and <code>2</code> are your chosen site ids.</p>

CC BY-SA 4.0
2021-07-12T08:46:29.733Z
2021-07-12T08:46:29.733Z
Oli
<p>I was able to get this working with this:</p>

<pre class="lang-php prettyprint-override"><code>$findTheNewEntry = Entry::find()
    -&gt;sectionId($sectionId)
    -&gt;id($entry-&gt;id)
    -&gt;anyStatus()
    -&gt;siteId($secondSiteId)
    -&gt;one();

$findTheNewEntry-&gt;enabledForSite = true;
Craft::$app-&gt;elements-&gt;saveElement($findTheNewEntry);
</code></pre>

<p>When creating a new entry via <code>-&gt;saveElement()</code>, Craft automatically creates entries for all sites that have that section enabled. The issues I was having were because <code>status</code> and <code>enabledForSite</code> are different, and I needed to use <code>-&gt;anyStatus()</code> to retrieve an element that was globally enabled when I created it but not enabled for the second site yet. Once that was retrieved, I could enable that entry for the second site as well.</p>

CC BY-SA 4.0
2019-04-11T12:43:31.060Z
2019-04-11T12:43:31.060Z
khamer
<p>My best guess is, that the duplicate element/ID pair gets thrown because after saving the first entry, the second site's entry already exists. This is because of the default behaviour of the <a href="https://docs.craftcms.com/api/v3/craft-services-elements.html#method-saveelement" rel="nofollow noreferrer"><code>saveElement()</code></a> method:</p>

<p><code>public function saveElement(ElementInterface $element, bool $runValidation = true, bool $propagate = true): bool</code></p>

<p>The third attribute (propagate) is set to true by default, resulting in Craft propagating your entry for site 1 to all other sites, depending on the propagation settings of that specific section. So, you could let this happen (and be sure to check what the default propagation settings for the section are), or tell <code>saveElement()</code> to not propagate:</p>

<pre><code># Save element without propagation, but with validation
Craft::$app-&gt;getElements()-&gt;saveElement($element, true, false)
</code></pre>

<p>Having said this, are you aware that, for Craft 3, you can now use the free and first party <a href="https://plugins.craftcms.com/feed-me" rel="nofollow noreferrer">Feed Me plugin</a> to import CSV?</p>

CC BY-SA 4.0
2019-04-11T12:39:03.570Z
I didn't realize that Feed Me was now first party - when I looked at Feed Me last, it couldn't be configured to the mapping I needed. Even now - I'm not sure it's possible to map a single row in the CSV to specify creating one entries for 2+ Sites.
khamer
CC BY-SA 4.0
2019-04-10T10:30:50.160Z
2019-04-10T10:30:50.160Z
Paul
<p>I've made a plugin that let's you import Entries from a CSV file and I'm working through adding localization (Sites) support to it, but I can't figure out the right way to enable an Entry for a second Site.</p>

<pre class="lang-php prettyprint-override"><code>$entry = New Entry();
$entry-&gt;authorId = $authorId;
$entry-&gt;sectionId = $sectionId;
$entry-&gt;typeId = $typeId;
$entry-&gt;enabled = true;
$entry-&gt;title = $title;
$entry-&gt;Slug = $slug;
$entry-&gt;siteId = $firstSiteId;

$entry-&gt;setFieldValues($attrs);

Craft::$app-&gt;elements-&gt;saveElement($entry);
</code></pre>

<p>Everything above works, but it's this next part which I can't get to work properly. It just throws errors about violating the constraint for a duplicate element ID/site ID pair:</p>

<pre class="lang-php prettyprint-override"><code>$entry-&gt;siteId = $secondSiteId;
Craft::$app-&gt;elements-&gt;saveElement($entry);
</code></pre>

<p>I also tried this, but the query never returns any results:</p>

<pre class="lang-php prettyprint-override"><code>$findTheNewEntry = Entry::find()
    -&gt;sectionId($sectionId)
    -&gt;id($entry-&gt;id)
    -&gt;status(null)
    -&gt;siteId($secondSiteId)
    -&gt;one();

$findTheNewEntry-&gt;enabled = true;
Craft::$app-&gt;elements-&gt;saveElement($findTheNewEntry);
</code></pre>

<p>I wish it was as simple as being able to provide an array or such for <code>-&gt;siteId</code> but I know that's wrong.</p>

<plugin-development><craft3><multi-site>
How do you save a new Entry to be enabled on 2+ Sites?
# How do you save a new Entry to be enabled on 2+ Sites?

(Asked by: username_0 on 2019-04-09)

I've made a plugin that let's you import Entries from a CSV file and I'm working through adding localization (Sites) support to it, but I can't figure out the right way to enable an Entry for a second Site.
$entry = New Entry();
$entry->authorId = $authorId;
$entry->sectionId = $sectionId;
$entry->typeId = $typeId;
$entry->enabled = true;
$entry->title = $title;
$entry->Slug = $slug;
$entry->siteId = $firstSiteId;

$entry->setFieldValues($attrs);

Craft::$app->elements->saveElement($entry);

Everything above works, but it's this next part which I can't get to work properly. It just throws errors about violating the constraint for a duplicate element ID/site ID pair:
$entry->siteId = $secondSiteId;
Craft::$app->elements->saveElement($entry);

I also tried this, but the query never returns any results:
$findTheNewEntry = Entry::find()
    ->sectionId($sectionId)
    ->id($entry->id)
    ->status(null)
    ->siteId($secondSiteId)
    ->one();

$findTheNewEntry->enabled = true;
Craft::$app->elements->saveElement($findTheNewEntry);

I wish it was as simple as being able to provide an array or such for ->siteId but I know that's wrong.

**Tags:** plugin-development, craft3, multi-site


---

## 3 answers

### Answer by username_1 on 2021-07-12. Score: 3

Since 3.4, you do this by passing an array of site ID/site-enabled mappings like so:
$entry->enabledForSite = [1 => true, 2 => true];

Where 1 and 2 are your chosen site ids.


---

### Answer by username_0 on 2019-04-11. Score: 3

I was able to get this working with this:
$findTheNewEntry = Entry::find()
    ->sectionId($sectionId)
    ->id($entry->id)
    ->anyStatus()
    ->siteId($secondSiteId)
    ->one();

$findTheNewEntry->enabledForSite = true;
Craft::$app->elements->saveElement($findTheNewEntry);

When creating a new entry via ->saveElement(), Craft automatically creates entries for all sites that have that section enabled. The issues I was having were because status and enabledForSite are different, and I needed to use ->anyStatus() to retrieve an element that was globally enabled when I created it but not enabled for the second site yet. Once that was retrieved, I could enable that entry for the second site as well.


---

### Answer by username_2 on 2019-04-10. Score: 1

My best guess is, that the duplicate element/ID pair gets thrown because after saving the first entry, the second site's entry already exists. This is because of the default behaviour of the saveElement() method:
public function saveElement(ElementInterface $element, bool $runValidation = true, bool $propagate = true): bool
The third attribute (propagate) is set to true by default, resulting in Craft propagating your entry for site 1 to all other sites, depending on the propagation settings of that specific section. So, you could let this happen (and be sure to check what the default propagation settings for the section are), or tell saveElement() to not propagate:
# Save element without propagation, but with validation
Craft::$app->getElements()->saveElement($element, true, false)

Having said this, are you aware that, for Craft 3, you can now use the free and first party Feed Me plugin to import CSV?

> **Comments:**
> **username_0**: I didn't realize that Feed Me was now first party - when I looked at Feed Me last, it couldn't be configured to the mapping I needed. Even now - I'm not sure it's possible to map a single row in the CSV to specify creating one entries for 2+ Sites.  

---

