3

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?

4

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! :)

  • 1
    What the freaking heck. That worked. Thanks! – Fred Jun 27 at 20:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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