Before save hook to fill 1:N relationship

in account module (primary module), I created a relationship 1: N with a custom module. (so in my custom module there is a field to populate with account).
Now, this field is hidden in edit mode, and it is possible to see it in detailview and in listview.

I would like populate this field, in beforesave logic hook, with parent_id for account selected in another relationship 1:N field. In logic hook. I made the query to retrieve the parent_id and add this code:
$bean->accounts_<mymodule>_2accounts_ida = $parentId;
but the field in detail, after save is empty. I check the table that contains this relationships, and is empty too.
How can I populate correctly this relation by code in logichook before save?
Thank you

have a look here:

basically, you need to initialize the relationship first, then you can use add():

1 Like

It works, thank you very much :slight_smile: