How to save data that has a one-to-one Relationship?

Hello Everyone,

I create two custom modules that have a one-to-one relationship. Then I created a custom UI for one of the custom modules, let’s say module (1), and added a form there where you can save data to module (2) using ajax and an entrypoint. The data is successfully saved to module (2), but there is no data added to the relationship table between module (1) and module (2).

Here `s my code on the entrypoint

    $Module2Bean = \BeanFactory::newBean('CSTM_Module2');
    $Module2Bean ->first_name = $_REQUEST['first_name'];
    $Module2Bean ->last_name = $_REQUEST['last_name'];
    $Module2Bean ->mobile_no = $_REQUEST['mobile_no'];
    $Module2Bean ->save();

Hope you able to visualize my concern :slight_smile:

Hey
You need to add a relationship with the first module and second module records

If you don’t know the name of the relationship between mod1 and mod2
go to studio → mod2 → relationships there you will find the name of the relationship between mod1 and mod2.
Use that in

$mod2Bean->load_relationship('relationship_name');
$mod2Bean->relationship_name->add($mod1);//$mod1 can be a bean or just the id of mod1 bean

@abuzarfaris this applies too on one-to-many relationship?

This is my scenarnio, I will update the status on module 1 then I will create record in the notes module, this wil serve as the status update tracking.

Yes it applies to one to many as well

@abuzarfaris , noted on this. It is possible also to define what values should be save on the other module?

I am not sure what you mean by define values
You can load the bean and put any values in it and save it

Like I want so save specific values on the relate module.

SAMPLE:
I have MODULE 1 that has a one-to-many relationship to MODULE 2, now I update the status information in MODULE 1(i.e from pending to for approval) then in the MODULE 2 It will create a record of this f.f (MODULE 1 Records Name, Status, Date/Time). The MODULE 2 will be the status event tracker of the MODULE 1.

I hope you were able to understand what I mean.

You can do it in a logic_hook before save or after save
check if value of status changed
create a new mod2 bean set values ,save it and add it in relationship of mod1 bean

or you can try doing using workflows. In the conditions add status and in the type column select any change
In actions create new record of mod2 and relate it to mod1

Hello @abuzarfaris , thank you for your suggestion. I was able to do it in workflows.