Hello!
I have a problem when saving a class thah has one-to-one relation. It saves just the TStockItem class, but none of the classes that are in one-to-one fields of the main class. No errors during save.
I'm sure that it's me doing something wrong, but I just can't find what
Please could you help?
Code (simplified) that produces the problem:
StockItem.Prices.DateChanged := Now;
try
if StockItem.Id<1 then begin
StockItem.EntryDate := now;
FManager.Save(StockItem);
end;
FManager.Flush(StockItem);
except
on E: Exception do begin
StockItem.Errors.Add('Interna napaka: '+E.Message);
end;
end;
Class definitions
[Entity]
[Table('product_prices')]
[Id('FId', TIdGenerator.None)]
TStockItemPrice = class(TBaseObject)
private
[Association([], CascadeTypeAllButRemove)]
[JoinColumn('PROD_ID', [TColumnProp.Unique, TColumnProp.Required, TColumnProp.NoUpdate], 'ID')]
FId: Proxy<TStockItem>;
...
function GetId: TStockItem;
procedure SetId(const Value: TStockItem);
public
property Item: TStockItem read GetId write SetId;
end;
function TStockItemPrice.GetId: TStockItem;
begin
Result := FId.Value;
end;
procedure TStockItemPrice.SetId(const Value: TStockItem);
begin
FId.Value := Value;
end;
[Entity]
[Table('products')]
[Id('FId', TIdGenerator.IdentityOrSequence)]
TStockItem = class(TBaseObject)
private
[Column('ID')]
FId: integer;
[ManyValuedAssociation([], CascadeTypeAllRemoveOrphan, 'FId')]
FPrices: Proxy<TList<TStockItemPrice>>;
function GetPrices: TStockItemPrice;
procedure SetPrices(const Value: TStockItemPrice);
public
constructor Create; reintroduce;
destructor Destroy; override;
property Id: integer read FId write FId;
property Prices: TStockItemPrice read GetPrices write SetPrices;
end;
function TStockItem.GetPrices: TStockItemPrice;
begin
if FPrices.Value.Count > 0 then
Result := FPrices.Value[0]
else
Result := nil;
end;
procedure TStockItem.SetPrices(const Value: TStockItemPrice);
begin
if FPrices.Value.Count = 0 then
FPrices.Value.Add(Value)
else
FPrices.Value[0] := Value;
end;
Database definition:
CREATE TABLE `products` (
`ID` INT(10) UNSIGNED AUTO_INCREMENT NOT NULL,
PRIMARY KEY (`ID`) USING BTREE
);
CREATE TABLE `product_prices` (
`PROD_ID` INT(10) UNSIGNED NOT NULL,
`PRICE_RETAIL` FLOAT(15,4) UNSIGNED NOT NULL DEFAULT '0.0000',
`DATE_CHANGE` TIMESTAMP NOT NULL DEFAULT current_timestamp(),
PRIMARY KEY (`PROD_ID`)
);