Tech Tip: Behavior when assigning values to a new entity
PRODUCT: 4D | VERSION: 17 | PLATFORM: Mac & Win
Published On: October 30, 2019
When a new entity is created using the .new() function, if a value is assigned to a field that has a one to many relation a query will be performed to populate the related many property of the entity.
For example:
If a new entity is created for Table_2 using the following:
$newEntity_ob:=ds.Table_2.new() |
An automatic query will be performed to populate the related many property of the entity if the field that has a related many is assigned a value.
From the names of the links, the related many property of the entity is "Link_2_return" after assigning a value to $newEntity_ob.T2_F2, $newEntity_ob.Link_2_return will contain an entity selection of all related entities from Table_3.
There is no a query performed to populate the related one property. Assigning a value to $newEntity.T2_F1 will not query and populate it's one property with a selection from Table_1
Commented by Justin Carr on October 30, 2019 at 5:21 PM
CAUTION: There is actually a query performed on the one table when you enter a value for T2_F1. Unfortunately it is querying for the one record which has the PKID value entered in T2_F1 even though the link is drawn to the T1_F1 field in the one table.
Then when you save the $newEntity_ob, the value in T2_F1 will be silently changed from what you have assigned, to the T1_F1 value of the one table record whose PKID field matches the value you assigned to T2_F1. This then permanently links the wrong records together with no way of knowing either that it has happened or how to undo it.