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

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

  • 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


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.