#1 2014-05-29 15:45:08

tech
Member
Registered: 2014-01-13
Posts: 110

Show DynArry with DrawGrid

Hi all,

I have the same Invoice class as in the documentation and I want to display the "Details: TInvoiceRecs" on a drawGrid, how can I do this ?

type
  TInvoiceRec = record
    Ident: RawUTF8;
    Amount: currency;
  end;
  TInvoiceRecs = array of TInvoiceRec;
  TSQLInvoice = class(TSQLRecord)
  protected
    fDetails: TInvoiceRecs;
    fTotal: Currency;
    procedure SetDetails(const Value: TInvoiceRecs);
  published
    property Details: TInvoiceRecs read fDetails write SetDetails;
    property Total: Currency read fTotal;
  end; 

Offline

#2 2014-05-29 18:37:22

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Show DynArry with DrawGrid

You can serialize them as JSON, then use JSONToDataSet() as defined in mORMotVCL.pas to link it to a grid.

Online

#3 2014-05-30 11:04:11

tech
Member
Registered: 2014-01-13
Posts: 110

Re: Show DynArry with DrawGrid

I tryed to serialize the DynArry as JSON but I get a Base64 string, I read the documentation but I don't understand how to do the conversion.

var json : RawUtf8;
    inv : TSQLInvoice;
    invDets : TInvoiceRecs;
begin
...
  try
    inv := := TSQLInvoice.CreateJoined(globalClient, fRec.ID); 
    invDets := inv.Details;    <----------------- No Base64
    TTextWriter.RegisterCustomJSONSerializer(TypeInfo(TInvoiceRecs), nil, nil);
    json := DynArraySaveJSON(invDets, TypeInfo(TInvoiceRecs));  <--------------- Base64
  finally
    inv.free;
  end;
...

Another question :
I added a TSQLArticle property into TInvoiceRec and some others, that works fine when adding records but when I try to load/retrieve the article information I got AV, should I retrieve true instances like with FillPrepareMany for TSQLRecordMany?

type
  TInvoiceRec = record
    Ident: RawUTF8;
    Article: TSQLArticle;
    Qty : Double;
    Amount: currency;
  end;
  TInvoiceRecs = array of TInvoiceRec;

I'm on Delphi7.

Offline

#4 2014-05-30 12:25:35

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Show DynArry with DrawGrid

First of all, you CAN NOT put an object within a record, then serialize it as JSON!
You can nest records or dynamic arrays, but not class instances!
Record are value objects, here.

Since Delphi 2010, you can serialize directly any record using enhanced RTTI.

For Delphi 7, you have to define your record type in text.
See http://blog.synopse.info/post/2013/12/1 … ialization
and the corresponding part of the documentation.

Something like that for you:

const
  __TInvoiceRec = 'Ident RawUTF8 Article TArticleRec Qty double Amount currency';
  __TArticleRec = '....';
...
TTextWriter.RegisterCustomJSONSerializerFromText(TypeInfo(TArticleRec),__TArticleRec);
TTextWriter.RegisterCustomJSONSerializerFromText(TypeInfo(TInvoiceRec),__TInvoiceRec);
// now Delphi 7 will serialize any TInvoiceRec or TInvoiceRecs values as JSON!

Here, TArticleRec being a record, not a class.

Online

#5 2014-05-30 14:15:34

tech
Member
Registered: 2014-01-13
Posts: 110

Re: Show DynArry with DrawGrid

Well, I think that I'll replace the sharding by a RecordMany class because in my business logic an invoice record can have many TSQLArticle instances.
I'm right or there's another way to do that ?

thnx for all explanations

Offline

#6 2014-05-30 15:41:30

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Show DynArry with DrawGrid

It depends.... on what you want to do.
See the SAD 1.18 pdf about sharding, and the MongoDB/NoSQL paragraphs.

Both paths (i.e. either sharding or RecordMany) do make sense!

Online

#7 2014-05-31 07:17:31

tech
Member
Registered: 2014-01-13
Posts: 110

Re: Show DynArry with DrawGrid

What I want to is simple, I have some classes like this
TsqlArticle=(Name, Brand, Family ...)
TsqlInvoice=(like documentation)
Each invoice can have several Articles (pivot table in RAD).
I want that the ui be able to add the invoice and details at once like in RAD. I'm using fastreport for some reasons and I need to serialize the invoice with details to dataset.

Offline

#8 2014-05-31 07:19:24

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Show DynArry with DrawGrid

Here you can use TSQLRecordMany .

Online

#9 2014-06-03 09:39:35

tech
Member
Registered: 2014-01-13
Posts: 110

Re: Show DynArry with DrawGrid

Hi AB,

I've transformed my sharding invoice details into TSQLRecordMany and it works correctly. Now I want to calculate some invoice totals automaticly after adding TSQLRecordMany records, is there a call back/event triggered after ManyAdd() function ?

thnx,

Offline

#10 2014-06-04 07:53:29

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Show DynArry with DrawGrid

On server side, you have TSQLRestServer.OnUpdateEvent which will be triggered after any adding.

Online

Board footer

Powered by FluxBB