1

Using PHP, I want to update a field of Entries type of an existing entry.

Fields of type Text and Number will update with the following code:

$entry = Entry::find()->id($entryId)->one();
$entry->textField = 20; // works
$entry->entriesField = Entry::find()->id($relatedEntryIds)->all(); // doesn't work
Craft::$app->elements->saveElement($entry);

I looked into the source code and it seems to be expecting a craft\elements\db\EntryQuery object but I can't get it to work.

Can anyone please help?

2

Ok this was easy. Tried it at first but didn't work and was successful after another go. All it needed was a array of Entry IDs.

Working code:

$entry = Entry::find()->id($entryId)->one();
$entry->textField = 20; // works
$entry->entriesField = [$relatedEntryId1, $relatedEntryId2]; // works
Craft::$app->elements->saveElement($entry);
| improve this answer | |

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.