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? Commented Jan 18, 2024 at 11:52
  • i am building a translation plugin. this code triggers on after save event. Commented Jan 19, 2024 at 11:28
  • Are you sure Craft's native propagation settings won't do what you're attempting? They're very flexible. Commented Jan 25, 2024 at 19:52

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.