1

I'm familiar with using FeedMe to import into Matrix blocks, 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 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 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. Wonder if batch generation with uuidtools.com/generate/bulk is compliant?

1 Answer 1

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.)

1
  • 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 :) Jan 23, 2023 at 16:11

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.