4

I'm using a migration script to clear out the values for a field on one of my entries. The field is an Entry Field and is not required.

So I do this:

$product->components = [];
Craft::$app->elements->saveElement($product);

But I get this error:

Argument 1 passed to craft\fields\BaseRelationField::_all() must implement interface craft\elements\db\ElementQueryInterface, array given, called in /var/www/html/vendor/craftcms/cms/src/fields/BaseRelationField.php on line 382

I can clear out other fields, even entry fields, just fine. But this particular one gives me this error every time.

I've also tried the following:

$product->components = null;
Craft::$app->elements->saveElement($product);

And:

$product->components = false;
Craft::$app->elements->saveElement($product);

Any ideas?

Tony DeStefano
  • 390
  • 1
  • 12

1 Answers1

5

I figured it out. I'm not sure understand the why of it. But here is the how:

$product->setFieldValue('components', []);
Craft::$app->elements->saveElement($product);

I guess setFieldValue() does some other stuff in it that the magic methods do not.

The more you know! :)

Tony DeStefano
  • 390
  • 1
  • 12