<p>I am trying to save entries data from code, but it's only working sometimes. Can you check what's wrong?</p>
<pre class="lang-php prettyprint-override"><code>$transaction = Craft::$app-&gt;getDb()-&gt;beginTransaction();

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

        Craft::info('Entry saved successfully.', __METHOD__);
    } else {
        // Handle saving error
        Craft::error('Failed to save translated entry.', __METHOD__);
    }
    $transaction-&gt;commit();
} catch (\Throwable $e) {
    // Handle transaction failure
    $transaction-&gt;rollBack();
    Craft::error('Transaction failed: '.$e-&gt;getMessage(), __METHOD__);
}
</code></pre>
<p>When I save one entry, I am saving them in some other sites.</p>

CC BY-SA 4.0
2024-01-18T11:52:23.143Z
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
CC BY-SA 4.0
2024-01-19T11:28:06.683Z
i am building a translation plugin. this code triggers on after save event.
Syeda Nusraht Khaledur
CC BY-SA 4.0
2024-01-25T19:52:17.723Z
Are you sure Craft's native propagation settings won't do what you're attempting? They're very flexible.
Brad Bell
<craft4>
Save entity data from code
# Save entity data from code

(Asked by: username_0 on 2024-01-18)

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.

**Tags:** craft4

> **Comments:**
> **username_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?  
> **username_0**: i am building a translation plugin. this code triggers on after save event.  
> **username_2**: Are you sure Craft's native propagation settings won't do what you're attempting? They're very flexible.  

---

## 0 answers

