hasoneedit
hasoneedit copied to clipboard
Creates new records instead of saving to existing record
Imagine Team
has one Coach
(one-to-one relationship). On first save in Team, it creates the Coach
record. But on each consecutive save, it creates a new coach record instead of saving to the one that's created.
See: http://stackoverflow.com/questions/41653975/silverstripe-single-one-to-one-objects-bug
I did some testing and took one step closer to the issue.
This bug is only in effect when you create multiple has_one relations of the same class type.
class Car extends DataObject {
private static $has_one = array(
'Owner' => 'Person',
'Driver' => 'Person' // two relations of the same class type
);
}
This confuses the mod and makes it produce new records on each save rather than saving into the already existing record.
Will see if I can solve the issue in the code. Help and tips are appreciated
I did another test, creating two new pseudo classes that extend Person
:
-
Person_One
extendsPerson
-
Person_Two
extendsPerson
and changing the Car
relation to:
private static $has_one = array(
'Owner' => 'Person_One',
'Driver' => 'Person_Two'
);
This does not change the situation: the mod keeps creating new records exactly in the same fashion.
If you didn't it yet, try to use the dot notation with $belongs_to
to specify the relation referrals. Take a look to the official doc.