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.