hasoneedit icon indicating copy to clipboard operation
hasoneedit copied to clipboard

Creates new records instead of saving to existing record

Open faloude opened this issue 8 years ago • 3 comments

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

faloude avatar Jan 14 '17 19:01 faloude

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

faloude avatar Jan 15 '17 14:01 faloude

I did another test, creating two new pseudo classes that extend Person:

  • Person_One extends Person
  • Person_Two extends Person

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.

faloude avatar Jan 24 '17 21:01 faloude

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.

ghost avatar Apr 24 '18 08:04 ghost