<p>Your best bet would be to update the Matrix block using the PHP APIs, rather than editing DB values directly.</p>
<p>You can do that like so:</p>
<pre class="lang-php prettyprint-override"><code>use Craft;
use craft\elements\MatrixBlock;

$block = MatrixBlock::find()
    -&gt;id($blockId)
    -&gt;fieldId($matrixFieldId)
    -&gt;one();

$block-&gt;setFieldValue('fieldHandle', $newValue);
// ...

Craft::$app-&gt;elements-&gt;saveElement($block);
</code></pre>
<p>(Note that setting the <code>fieldId</code> param on the Matrix block query is necessary. Oterwise the block won’t be queried with any of its content.)</p>

CC BY-SA 4.0
2023-01-23T16:11:12.407Z
Thanks Brandon. That sounds helpful. Had just got to the point where my attempts at INSERT INTO `relations` were not working. (I guess there are joins of some sort I don't know about which I am not making.) So I'll give your suggestion a go :)
Jonathan Schofield
CC BY-SA 4.0
2023-01-23T12:52:40.337Z
2023-01-23T12:52:40.337Z
Brandon Kelly
<p>I'm familiar with <a href="https://docs.craftcms.com/feed-me/v4/guides/importing-into-matrix.html" rel="nofollow noreferrer">using FeedMe to import into Matrix blocks</a>, but I’m not sure how to do the following:</p>
<ul>
<li>I have a series of existing entries, each with a series of existing Matrix blocks of several types</li>
<li>I need to update one field of one block type <em>without affecting the others already in the Matrix field.</em></li>
</ul>
<p>I've just done a test of what I hoped might work, being careful to make sure that <code>Don’t import</code> was the setting for <em>all other fields</em> across the entire entry scope.</p>
<p><em>But it wiped out the other existing blocks.</em> (I had a backup!)</p>
<p>I’ve generated a report to acquire the <code>block.id</code> of the specific blocks I need to update but I'm not sure where to go from here.</p>
<p>There doesn't seem to be a way to use FeedMe to update blocks directly?</p>
<p>Do I need to resort to some kind of SQL?</p>
<hr />
<p>Note: it's not going to be practical to acquire all the existing block data and incorporate that into the import. Or at least, I'd rather not!</p>
<hr />
<h2>Update</h2>
<p>It appears this issue is an intrinsic part of the <a href="https://github.com/craftcms/feed-me/issues/463" rel="nofollow noreferrer">Matrix content gets overwritten on import #463</a> 2019 thread and there is no answer as yet?</p>
<blockquote>
<p>“Feed Me doesn’t have a way of matching your feed’s blocks with existing entry blocks, so it will always just delete the current entry blocks and recreate them with whatever’s in the feed.”</p>
</blockquote>
<p>FWIW I think <a href="https://github.com/craftcms/feed-me/issues/463#issuecomment-998953478" rel="nofollow noreferrer">Eli</a> describes it fairly with…</p>
<blockquote>
<p>“Whether or not this is ‘expected’ from a programmatic standpoint, it's definitely not what I would expect as a user – even one who is pretty familiar with Craft's internals.”</p>
</blockquote>
<p>Hope this can be overcome in a future release.</p>
<hr />
<h2>Update 2</h2>
<p>Just had a look at the DB and, as I know the block IDs, it looks promising that I can do an SQL UPDATE on the <code>matrixcontent_myfieldhandle</code> table, but as the field to be updated is an Entries field it could get complicated!</p>
<hr />
<h2>Update 3</h2>
<p>Because my field is an <code>Entries</code> field it looks like I need to SQL INSERT into the <code>relations</code> table, which looks straightforward enough, as long as I can play nicely with <a href="https://craftcms.stackexchange.com/a/6927/3507">values for the UID column</a>. Wonder if batch generation with <a href="https://www.uuidtools.com/generate/bulk" rel="nofollow noreferrer">uuidtools.com/generate/bulk</a> is compliant?</p>

<matrix><plugin-feedme><updates>
FeedMe import to update specific Matrix blocks without affecting others
# FeedMe import to update specific Matrix blocks without affecting others

(Asked by: username_0 on 2023-01-20)

I'm familiar with [using FeedMe to import into Matrix blocks](https://docs.craftcms.com/feed-me/v4/guides/importing-into-matrix.html), but I’m not sure how to do the following:

* I have a series of existing entries, each with a series of existing Matrix blocks of several types
* I need to update one field of one block type *without affecting the others already in the Matrix field.*

I've just done a test of what I hoped might work, being careful to make sure that `Don’t import` was the setting for *all other fields* across the entire entry scope.

*But it wiped out the other existing blocks.* (I had a backup!)

I’ve generated a report to acquire the `block.id` of the specific blocks I need to update but I'm not sure where to go from here.

There doesn't seem to be a way to use FeedMe to update blocks directly?

Do I need to resort to some kind of SQL?

---

Note: it's not going to be practical to acquire all the existing block data and incorporate that into the import. Or at least, I'd rather not!

---

## Update

It appears this issue is an intrinsic part of the [Matrix content gets overwritten on import #463](https://github.com/craftcms/feed-me/issues/463) 2019 thread and there is no answer as yet?

> “Feed Me doesn’t have a way of matching your feed’s blocks with existing entry blocks, so it will always just delete the current entry blocks and recreate them with whatever’s in the feed.”

FWIW I think [Eli](https://github.com/craftcms/feed-me/issues/463#issuecomment-998953478) describes it fairly with…

> “Whether or not this is ‘expected’ from a programmatic standpoint, it's definitely not what I would expect as a user – even one who is pretty familiar with Craft's internals.”

Hope this can be overcome in a future release.

---

## Update 2

Just had a look at the DB and, as I know the block IDs, it looks promising that I can do an SQL UPDATE on the `matrixcontent_myfieldhandle` table, but as the field to be updated is an Entries field it could get complicated!

---

## Update 3

Because my field is an `Entries` field it looks like I need to SQL INSERT into the `relations` table, which looks straightforward enough, as long as I can play nicely with [values for the UID column](https://craftcms.stackexchange.com/a/6927/3507). Wonder if batch generation with [uuidtools.com/generate/bulk](https://www.uuidtools.com/generate/bulk) is compliant?

**Tags:** matrix, plugin-feedme, updates


---

## 1 answers

### Answer by username_1 on 2023-01-23. Score: 1

Your best bet would be to update the Matrix block using the PHP APIs, rather than editing DB values directly.
You can do that like so:
use Craft;
use craft\elements\MatrixBlock;

$block = MatrixBlock::find()
    ->id($blockId)
    ->fieldId($matrixFieldId)
    ->one();

$block->setFieldValue('fieldHandle', $newValue);
// ...

Craft::$app->elements->saveElement($block);

(Note that setting the fieldId param on the Matrix block query is necessary. Oterwise the block won’t be queried with any of its content.)

> **Comments:**
> **username_0**: Thanks Brandon. That sounds helpful. Had just got to the point where my attempts at INSERT INTO `relations` were not working. (I guess there are joins of some sort I don't know about which I am not making.) So I'll give your suggestion a go :)  

---

