How to Access Parent Entity

How can I access the Parent Entity, without adding a Field to the Child-Entity?

Example:

[Entity, Automapping]
TChild
strict private
FId: Integer;
FName: string;
public
[OnDeleting]
procedure OnDeleting(aArgs: TDeletingArgs);

property Id: Integer read FId;
property Name: string read FName write FName;
end;

[Entity, Automapping]
TParent
strict private
FId: Integer;
FName: string;
[ManyValuedAssociation([TAssociationProp.Lazy], CascadeTypeAllRemoveOrphan)]
[ForeignJoinColumn('PARENT_ID', [])]
FChildren: Proxy<TList>;

function GetChildren: TList;
public
property Id: Integer read FId;
property Name: string read FName write FName;
property FChildren: TList read GetChildren;
end;

procedure TChild.OnDeleting(aArgs: TDeletingArgs);
begin
// How to Access TParent, for changing some Informations?
end;

I tried to add FParentId to TChild, but this raises an error, because UpdateDatabase then tries to add 2 Columns parent_id...
Another try, using DynamicProperties also raises the same error on UpdateDatabase.

I'm searching for this in your docs for a while, but I didn't find anything. (Same for your Demos)

You can't. You have to have the child entity.

This is because you have to tell Aurelius that one side of the association refers to the other side of the association. You do this by using the MappedBy parameter of the ManyValuedAssociation attribute.

This is explained in the ManyValuedAssociation documentation, more specifically this code snipped:

Example using MappedBy parameter:

TMediaFile = class
private
  [Association([TAssociationProp.Lazy], [])]
  [JoinColumn('ID_ALBUM', [])]
  FAlbum: Proxy<TAlbum>;

TAlbum = class
public
  [ManyValuedAssociation([], CascadeTypeAllRemoveOrphan, 'FAlbum')]
  property MediaFiles: TList<TMediaFile> read FMediaFiles write FMediaFiles;

Well I didn't get this using of MappedBy. That was my mistake.
I did something like this:

TMediaFile = class
private
  [Association([TAssociationProp.Lazy], [])]
  [JoinColumn('ID_ALBUM', [])]
  FAlbum: Proxy<TAlbum>;

TAlbum = class
public
  [ManyValuedAssociation([], CascadeTypeAllRemoveOrphan)]
  [ForeignJoinColumn('MEDIA_FILES')]
  property MediaFiles: TList<TMediaFile> read FMediaFiles write FMediaFiles;

and this just won't work.

Thank you for this solution

1 Like

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.