Adding relationship does not work

Hello everyone, I briefly explain my problem:
I have custom modules related to each other, I need to add a relationship to one module when in another module I create a relationship (by creating a relationship I add others). Apparently the relationships are created (I can see it from the DB), but only one is set with deleted=0 field, the others have deleted=1 and I don’t understand why.
Trying to understand the problem step by step, I made a test by eliminating all foreach loops and creating an array of only 2 ids of the modules that must be part of the relationship, adding this array, again only one element is added (even if both appear in the DB, but one is set to deleted=1).
The procedure is this:

logic_hooks.php:
$hook_array[‘after_save’] = Array();
$hook_array[‘after_save’][] = Array(1, ‘afterSave’, ‘custom/modules/ModuleA/relation.php’, ‘relation’, ‘afterSave’);

relation.php:
$bean->load_relationship(’<moduleA_moduleB>’);
$pz=array(‘id_1’,‘id_2’);
$bean-><moduleA_moduleB>->add($pz);

If I load an id individually, it is added normally, but if I try to insert them via a loop or a group (array or bean), only one is added, the other, as mentioned, although added, its deleted field is set to 1.
Can someone please tell me the reason for this behavior? I am going crazy. Thanks

I’ve always passed a single Bean object into those add functions, like this:

$bean1->relName->add($bean2);

Where did you get that idea of sending it an array of ids?

@fab

What relationship do you use: one-many or many-many?

Don’t be crazy. Everything will be fine.

@pgr

It’s possible. Look at the function add in file data/Link2.php .

1 Like

@pgr
I didn’t know it either, but I saw the Suite documentation that talked about it and I used it, anyway I also tried to cycle, adding every single bean, but the result is the same

@p.konetskiy
I use one-many relationship

@fab

Maybe the problem that your $bean is side one of link. The second or last link remove previous link every time.

and if so? How do I fix it?

@fab

It is not code mistake. You should change logic. Your record of moduleA can have only one link to moduleB but you coding several links. I don’t know anything about logic but in coding you can:

  • are adding only one record;
  • change relationship from one-many to many-one (change link between modules)
  • change relationship from one-many to many-many

@p.konetskiy @pgr
thanks a lot, the problem was just the relationship, I changed from one-to-many to many-to-many and now it works perfectly

1 Like