How to clone entity values

Hi,

is it possible and there is a quick way to clone the entire data content of ID 11111, but only replace the contents of certain fields before saving with new data. as in the code that I display.

  [Entity]
  [Table('TRX_DEP')]
  [Sequence('GEN_TRX_DEP_ID')]
  [Id('FID', TIdGenerator.IdentityOrSequence)]
  TTRX_DEP = class
  private
    [Column('ID', [TColumnProp.Required])]
    FID: Integer;

    [Column('PRICE_VAL', [TColumnProp.Required])]
    FPRICE_VAL: Double;
	
	[Column('DATE_TRX', [TColumnProp.Required])]
    FDATE_TRX: TDateTime;
	.
	.
	.
	.
	.
	.
  public
    property ID: Integer read FID write FID;
	property PRICE_VAL: Double read FPRICE_VAL write FPRICE_VAL;
	property DATE_TRX: TDateTime read FDATE_TRX write FDATE_TRX;
	.
	.
	.
	.
	.
	.
  end;
  
  
  
 var
  TrxDep: TTRX_DEP;
begin
  TrxDep := Manager.Find<TTRX_DEP>(11111);

  //change some values
  TrxDep.PRICE_VAL := -TrxDep.PRICE_VAL;
  TrxDep.DATE_TRX := Now;
  Manager.Save(TrxDep);
end;

Thank You

now i do it this way

var
  TrxDepList: TList<TTRX_DEP>;
  TrxDep: TTRX_DEP;
   dValue: Double;
begin
  TrxDepList := Manager.Find<TTRX_DEP>
                        .Where((Linq['ID'] = 11111))
                        .List;
  try
    dValue := TrxDepList[0].PRICE_VAL;
    TrxDep.PRICE_VAL := -dValue;
    TrxDep.DATE_TRX := Now;
    Manager.Save(TrxDep);
  finally
    TrxDepList.Free;
  end;
end;

To modify properties of a specific object, don't use Save method, use Flush:

TrxDep.DATE_TRX := Now;
Manager.Flush(TrxDep);

sorry for my bad explanation, I meant to create new data from existing data. as I do right now as in my second post above

Your second post doesn't do that. It should raise an error. The entity passed to the Save method shouldn't have an id.
Best option is to set the id to 0 and call replicate:

TrxDep.ID := 0;
ClonedTrxDep := Manager.Replicate(TrxDep);

thanks Wagner, i will try it.

you are right, when I did it in a vcl application with aurelius this code doesn't work. but if i do it on xdata server, this code works fine.

TrxDepList := Manager.Find<TTRX_DEP>
                        .Where((Linq['ID'] = 11111))
                        .List;
TXDataOperationContext.Current.Handler.ManagedObjects.Add(TrxDepList);
if TrxDepList.Count = 0 then
   raise EXDataHttpException.Create(403, 'data not found');

dValue := TrxDepList[0].PRICE_VAL;
TrxDep.PRICE_VAL := -dValue;
TrxDep.DATE_TRX := Now;
Manager.Save(TrxDep);

what makes it work on xdata server?

Note that both you original code and XData code are not correct, but I was not sure if it was pseudo-code or partial code, so I ignore id.
But your TrxDep variable is not initialized.

Yes, that is true, that is the partial code that I typed when making a post, so there is a wrong or missing code. I attach this sample project that I created to show that the code above is working properly.

Replicate.zip (3.2 MB)

You are creating a new instance and calling save. That's just plain simple Aurelius usage, it will insert a new record in database:

  TrxDep := TTRX_DEP.Create;
  TrxDep.PRICE_VAL := -dValue;
  TrxDep.DATE_TRX := Now;
  TrxDep.EMAIL := TrxDepList[0].EMAIL;
  TrxDep.DESCRIPTION := TrxDepList[0].DESCRIPTION;
  TrxDep.CHANNEL := TrxDepList[0].CHANNEL;
  TrxDep.ID_TRX_CORRECTION := TrxDepList[0].ID;
  Manager.Save(TrxDep);
end;