4

I'm trying to import data from another database using locales, but am having trouble saving the other locale after the primary locale is saved.

foreach($row as $data) {

    $entry = new EntryModel();
    $entry->sectionId   = 1;
    $entry->typeId      = 1;
    $entry->authorId    = 1;
    $entry->enabled     = true;
    $entry->locale      = "en";

    $entry->getContent()->setAttributes(array(
        'description' => $data["description-en"],
    ));

    $success = craft()->entries->saveEntry($entry);

    if ($success) 
    {
        ImportPlugin::log('Successfully saved entry "'.$entry->id.'"', LogLevel::Info);

        $entryEs = craft()->entries->getEntryById($entry->id, "es");
        $entryEs->getContent()->setAttributes(array(
            'description'   => $data["description-es"],
        ));

        // **** The following doesn't work for some reason ****

        craft()->entries->saveEntry($entryEs);
    };
}

The primary entry saves fine and if I dump vars I can see that both $entry.id and $entryEs exist and are formatted as expected — but it doesn't save.

Is it perhaps being overwritten (or ignored) as the content from the primary locale is being copied to the other locales in the background? What's the recommended way to save multiple locales?

2
  • Not sure I have an answer but it looks like you are setting the locale to a hard coded 'en' in this line: $entry->locale = "en"; and you are trying to retrieve an entry by asking for the id 'es' in this line $entryEs = craft()->entries->getEntryById($entry->id, "es"); – Ben Parizek Feb 20 '15 at 5:50
  • Thanks Ben. Yes.. confusing I know, but that was the point. I first create the entry in english, which generates an entry in all 3 locales. I then retrieve the spanish version (entryEs); set one of the Spanish fields; and save — but no luck. I can even confirm that entryEs exists, and that the content is set, but it just doesn't save. Very odd. I ended up doing it in two steps using a temporary field on the english version, that I then later copied over to the Spanish version in a second pass. Works, but not ideal. – Douglas McDonald Feb 20 '15 at 6:48
2

I just tried this and it works with Craft 2.3.2636. The only difference is this isn't using ContentModel::setAttributes(), but I wouldn't have thought that would affect it.

$entry = new EntryModel();
$entry->sectionId   = 2;
$entry->typeId      = 2;
$entry->authorId    = 1;
$entry->enabled     = true;
$entry->locale      = "en";

$entry->getContent()->title       = "English Title";
$entry->getContent()->description = "English description.";

if (craft()->entries->saveEntry($entry))
{
    $entryEs = craft()->entries->getEntryById($entry->id, "es");

    $entryEs->getContent()->title       = "Spanish Title";
    $entryEs->getContent()->description = "Spanish description.";

    craft()->entries->saveEntry($entryEs);
}
2
  • Thanks Mike. I'm unable to test this at the moment, but I'll take your word for it and mark as correct. Thanks. – Douglas McDonald Apr 1 '15 at 17:13
  • You may also want to use $entryEs->localeEnabled = true; to make sure the entry is enabled in that language – Martin Jun 1 '15 at 15:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.