0

I am trying to save entries data from code, but it's only working sometimes. Can you check what's wrong?

$transaction = Craft::$app->getDb()->beginTransaction();

try {
    // Save the translated entry
    if (Craft::$app->elements->saveElement($targetEntry)) {
        // Optionally, propagate the changes to other sites
        $entryQuery = Entry::find()->id($targetEntry->id);
        Craft::$app->elements->propagateElements($entryQuery);

        Craft::info('Entry saved successfully.', __METHOD__);
    } else {
        // Handle saving error
        Craft::error('Failed to save translated entry.', __METHOD__);
    }
    $transaction->commit();
} catch (\Throwable $e) {
    // Handle transaction failure
    $transaction->rollBack();
    Craft::error('Transaction failed: '.$e->getMessage(), __METHOD__);
}

When I save one entry, I am saving them in some other sites.

3
  • 1
    What so you want to achieve with this code? You can already configure propagation options and translation settings in Craft, why do you need this code? When does it run? And in what way is it not doing what you expect? What can you see in the error logs?
    – MoritzLost
    Commented Jan 18 at 11:52
  • i am building a translation plugin. this code triggers on after save event. Commented Jan 19 at 11:28
  • Are you sure Craft's native propagation settings won't do what you're attempting? They're very flexible.
    – Brad Bell
    Commented Jan 25 at 19:52

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.