You are not logged in.
Pages: 1
Hi Guys,
Considering these two related records:
TSQLUser...
property Name: RawUTF8;
TSQLProduct...
property Title : RawUTF8;
property User : TSQLUser;
ORM return the JSON for Joined methods in this way:
{
Product.ID: 1,
Product.Title: Test,
User.ID: 10,
User.Name : Jhon
}
Is there any way to return in this format?
{
Product: {
ID: 1,
Title: Test...
},
User :{
ID: 10,
Name : Jhon
}
}
Offline
Yes, both CreateJoined and CreateAndFillPrepareJoined dont return related records as JSON objects.
AProduct := TSQLProduct.CreateJoined(Client,1);
SynSQLite3.TSQLDatabase(01ABA6D0) [{"Product.RowID":1,"Product.CreatedAt":135451071709,"Product.ModifiedAt":135451071709,"User.RowID":1,"User.CreatedAt":135451071709,"User.ModifiedAt":135451071709}]
Last edited by macfly (2018-07-03 18:13:06)
Offline
Of course!
What you see here is the result of the JOINed SQL query.
So field names have been "flattened", as expected.
If you want the nested JSON objects, then use ObjectToJSON(AProduct) after it has been properly filled...
And anyway, I would almost never use such joined for nested objects, but add some variant properties containing TDocVariant JSON content.
That is, aggregate documents may have a better use than joined tables, when you work with objects.
Offline
Thanks.
I need to return an array of json with nested objects, and using this approach works:
AProdList := TList.Create;
try
AProduct := TSQLProduct.CreateAndFillPrepareJoined(Server,'WHERE ... LIMIT...',[],[]);
try
while AProduct.FillOne do
AProdList.Add(AProduct);
Result := ObjectToJSON(AProdList);
finally
AProduct.Free;
end;
finally
AProdList.Free;
end;
It is not easy to break the paradigm and use only sharding.
But I'm trying
Last edited by macfly (2018-07-04 02:23:13)
Offline
Your code will randomly trigger access violations, since you make AProduct.Free whereas AProduct is still in AProdList then used later on...
You should better use a TObjectList owning the items.
Offline
This is not the actual code, but thanks for the note.
Fix it up there
Offline
Pages: 1