Hiding field type

Hi,
I have annoying issue with our DB design.
We have dates in few tables presented as yyyymmdd string. (pure string)
We would love to switch them to real db's own date field but that requires a lot of change and
we cannot delay api so long.
My wish was that i would have declared it something like this
[FColumn('invoicedate') ];
Finvoicedatestr: string;
[Calculated]
Finvoicedate: Tdatetime;
Property InvoiceDate: TdateTime read FInvoiceDate Write SetInvoiceDate;
When data is retrieved from tables
aurelius would invoke "afterLoadFromdataset" event;
When InvoiceDate is set trough Xdata InvoiceData Setter is invoked.
so in json it would be presented like ..., "invoicedatestr":"20201218", "invoicedate":"2020-12-18", ...

If that would work we could at same moment in future update db structure and switch declarations so that FInvoiceDateStr would be calculated and FinvoiceDate would be db field.

But you can do that right now. Just create the properties and fields as you mentioned. Do not forget to also set GetInvoiceDate, to translate from FInvoicedatestr to the TDateTime format.

Really?
I can't get my InvoideDate in xdata json output.
Class declaration (NO Automapping)

    [ Column( 'toimasmaa', [ ], 20 ) ]
    Fdeliveryaddresscountry: nullable< string >;
    FinvoiceDate: Tdatetime;
    [ Column( 'lptila' ) ]
    Finvoiceservicestatus: nullable< Integer >;
...
    property deliveryaddresscountry: nullable< string >
      Read Fdeliveryaddresscountry
      Write Fdeliveryaddresscountry;
    property invoicedate: Tdatetime
      read Getinvoicedate
      write Setinvoicedate;
    property invoiceservicestatus: nullable< Integer >
      Read Finvoiceservicestatus
      Write Finvoiceservicestatus;

and output is

   "delivarycoaddress": "",
    "deliveryaddresscountry": "",
    "invoiceservicestatus": 0,

What I'm missing?

Hi there,

Sorry. Did not read your first message. If FInvoiceDate is not a persisted field, than to have it serialized you must use [XDataProperty] attribute:

[XDataProperty]  // this is missing
FinvoiceDate: Tdatetime;

I guess what you're really looking for (and Wagner pointed out) is something like:

  private
    [XDataExcludeProperty] 
    [FColumn('invoicedate')]
    FInvoiceDateStr: string;
  private
    function GetInvoiceDate: TDateTime;
    procedure SetInvoiceDate (const AValue: TDateTime);
  public
    [XDataProperty]
    property InvoiceDate: TDateTime read GetInvoiceDate write SetInvoiceDate;

(* ... *)

function TSomeObject.GetInvoiceDate: TDateTime;
var
  fs: TFormatSettings;
begin
  // set fs format as desired
  Result := StrToDate(FInvoiceDateStr, fs);
end;

procedure TSomeObject.SetInvoiceDate(const AValue: TDateTime);
begin
  FInvoiceDateStr := FormatDateTime('some-date-format', AValue);
end;

HTH,

1 Like

No, cause Finvoicedate should be calculated from Finvoicedatestr

2 Likes

Thanks, this helped.
My previous question: What I'm missing? answer: ability to read... :smiley:

2 Likes

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