I am currently trying to update entries for a user. The problem is: If i am saving a new relation, it's overriding the one before.

My .csv

customUserId   customEnryId
22             1000
22             2000
foreach ($csv as $record) {
    $user = User::find()->search('customUserId:' . $record[0])->one();
    $customEntry = Entry::find()->section('mySection')->search('customEntryId:' . $record[1])->one();

    $array = [

    $field = Craft::$app->fields->getFieldByHandle('customField');

    Craft::$app->relations->saveRelations($field, $user, $array);

So the last entry is always saved, while the entry before ( 1000 ) is overwritten and not actually pushed to the existing relations.

Or do i really need to get all relations first before i save a new one and i can't really directly push a new relation to the user?

So like this - Because this is working, but is this the way to go?

$currentEntries = [];
foreach ($user->customEntry as $item) {
    $currentEntries = $item->id;

$array = [

Your Answer

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

Browse other questions tagged or ask your own question.