You are not logged in.
Pages: 1
Hello!
The code below works on mORMot 1 but not on version 2...
procedure TForm1.Button1Click(Sender: TObject);
var
MSC_mem: TRestStorageInMemory;
MSR_table: TOrmUser;
begin
MSC_mem := TRestStorageInMemory.Create(TOrmUser, nil, '');
MSR_table := TOrmUser.Create;
MSR_table.FillPrepare(MSC_mem, '');
// Add a Record
MSR_table.SetFieldVariant('Name', 'Paula');
MSR_table.SetFieldVariant('Age', 48);
MSC_mem.Add(MSR_table, true, false, false);
// Save JSON
MSR_table.FillPrepare(MSC_mem, '');
Memo1.Lines.Text := MSR_table.FillTable.GetJSONValues(true);
MSC_mem.Free;
end;
I get an error on line: MSC_mem.Add(MSR_table, true, false, false);
(Access violation error)
What is the problem?
I will use this to convert some data to JSON, multiple records.
Thank you.
Last edited by imperyal (2024-04-05 12:27:05)
Offline
TRestStorageInMemory requires to be used with a TRestOrmServer instance and its associated TRestModel.
Use TRestServerFullMemory for a stand-alone storage, e.g. as TRestServerFullMemory.CreateWithOwnModel([TOrmUser]).
Then use its Orm property to work as abstract as possible with its IRestOrm associated interface.
Offline
It is working now... Thank you ab.
Please just confirm that this is correct...
procedure TForm1.Button1Click(Sender: TObject);
var
MSC_mem: TRestServerFullMemory;
MSR_table: TOrmUser;
begin
MSC_mem := TRestServerFullMemory.CreateWithOwnModel([TOrmUser]);
MSR_table := TOrmUser.Create;
MSR_table.FillPrepare(MSC_mem.Orm, '');
// Add a Record
MSR_table.ClearProperties;
MSR_table.SetFieldVariant('Name', 'Paula');
MSR_table.SetFieldVariant('Age', 48);
MSC_mem.Add(MSR_table, true);
// Add another Record
MSR_table.ClearProperties;
MSR_table.SetFieldVariant('Name', 'Maria');
MSR_table.SetFieldVariant('Age', 48);
MSC_mem.Add(MSR_table, true);
// Save JSON
MSR_table.FillPrepare(MSC_mem.Orm, '');
Memo1.Lines.Text := MSR_table.FillTable.GetJSONValues(true);
MSR_table.Free;
MSC_mem.Free;
end;
Last edited by imperyal (2024-04-05 15:11:41)
Offline
Perhaps TDocVariant or IDocList may be a better approach to process JSON without any format.
But your code seems fine.
Just take care that perhaps using IRestOrm is always better than directly access the TRestServerFullMemory, as I wrote above.
Note that if you use a IRestOrm instance, you have to properly set it to nil before calling TRestServerFullMemory.Free.
Offline
I added the IRestOrm thing. This is a non critical part of the client code so I will let it stay this way (and it is working).
Thank you!
Offline
Pages: 1