#1 2016-07-12 05:05:02

wiltomar
Member
Registered: 2016-07-12
Posts: 1

mORMot ORM (One-to-many) in pratice

Good morning, folks.

I'm needing develop a app for make a json file, but I don't know how create a equal model below, with a TPessoa and many sons of the type TPessoaFisica in one result.
I need a pratice example of use the TSQLRecord and TSQLRecordMany, for applying in my model.

Example of classes:

TPessoa = class;
TPessoaFisica = class;
TPessoasFisicas = class;

TPessoa = class(TSQLRecord)
private
   fnome: string;
   fapelido: string;
   fpessoasfisicas: TPessoasFisicas;
published
   property nome: string read fnome write fnome;
   property fapelido: string read fapelino write fapelido;
   property fpessoasfisicas: TPessoasFisicas read fpessoasfisicas write fpessoasfisicas;
end;

TPessoaFisica = class(TSQLRecord)
private
  fnascimento: TDateTime;
  fpessoa: TPessoa;
published
  property nascimento: TDateTime read fnascimento write fnascimento;
  property pessoa: TPessoa read fpessoa write fpessoa;
end;

TPessoasFisicas = class(TSQLRecordMany)
private
  fsource: TPessoa;
  fdest: TPessoaFisica;
published
  property source: TPessoa read fsource write fsource;
  property dest: TPessoaFisica read fdest write fdest;
end;

Please, someone help me.

Regards for all.

Wiltomar DUARTE
A brasilian developer.

Offline

#2 2016-07-12 05:30:46

edwinsn
Member
Registered: 2010-07-02
Posts: 1,215

Re: mORMot ORM (One-to-many) in pratice

I handle the one-to-many relationships outside of the mORMot, for flexibility, freedom and clarity.

I mean, define the classes like this:

TSQLItem = class(TSQLRecord)
published:
  property HeaderId: TID; // Reference to the master table
end;

TSQLHeader = class(TSQLRecord)
public // not published
  Items: TObjectList<TSQLItem>;
end;

We saving, the program set TSQLItem.HeaderId to the ID of the corresponding header.

When retrieving, the program load the header and item separately, something like:

  myDb.Retrieve(myHeader, aHdrId);
  myHeader.Items := myDb.RetrieveList<TSQLItem>('HeaderID = ?', [], [aHdrId]);

I'm not sure what others think, but to me the built-in support for one-to-many relationship provided by the framework is too limited and not flexible.

And this is why I like mORMot - it finds a very good balance between "an ORM that completely abstract away the relational database concept" and "Directly use TDataSet". In other words, it does the tedious CRUD work excellently for me, while also let me easily take advantages of the proven SQL concept.


Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.

Offline

Board footer

Powered by FluxBB