#1 2016-10-27 19:32:46

JD
Member
Registered: 2015-08-20
Posts: 101

Modeling 1 to many relationships

Hi there everyone,

After much effort (thanks to ab and DonAlfredo/AOG (- fpcup is really great!)), I succeeded in getting mORMot to work for me on my Lazarus/FPC installations (Lazarus 1.6/FPC 3.0 and Lazarus 1.7/FPC 3.1.1).

I am simply overwhelmed at how the framework is put together and how much time it could save me with development once I get the data model right. I spend long hours writing and optimizing SQL code so you can imagine how I feel.

I have been reading the documentation as well as trying the examples on the site https://tamingthemormot.wordpress.com/2 … tionships/. However I ran into a problem and I tried a workaround that worked but I'm not sure if I did the right thing.
My modified code with the workaround is shown below

//
procedure TestNoteCase(Rest: TSQLRest);
var
  aCase: NoteORM.TNoteCase;
  aNote: NoteORM.TNote;
begin
  //
  aCase := NoteORM.TNoteCase.Create;
  //
  try
    //
    aCase.Description := 'Case 1';
    Rest.Add(aCase, True);
    //
    aNote := NoteORM.TNote.Create;
    //
    try
      aNote.Title     := 'Note 1';
      aNote.Body      := 'Note 1 body. Lots of other stuff too.';
      // Notes.lpr(63,31) Error: Incompatible types: got "Pointer" expected "TNoteCase"
      //aNote.NoteCase  := aCase.AsTSQLRecord;
      aNote.NoteCase  := TNoteCase(aCase.AsTSQLRecord);   <---- IS THIS OK?
      Rest.Add(aNote, True);
      //
      aNote.Title     := 'Note 2';
      aNote.Body      := 'Note 2 body. Lots of other stuff too.';
      // Save a nil to the record
      aNote.NoteCase  := TNoteCase(nil);                  <---- IS THIS OK?
      Rest.Add(aNote, True);
    finally
      aNote.Free;
    end;
  finally
    aCase.Free;
  end;
end;

The lines with <---- IS THIS OK? are the ones I'm not sure about. The original line

aNote.NoteCase  := aCase.AsTSQLRecord;

gave me an error

Error: Incompatible types: got "Pointer" expected "TNoteCase"

so I changed it to

aNote.NoteCase  := TNoteCase(aCase.AsTSQLRecord);

and I used

aNote.NoteCase  := TNoteCase(nil);

to save a nil or zero reference to the table because I found out that the second record retained the value of aNote.NoteCase from the first record.

It worked but am I doing the right thing?

Thanks,

JD

Last edited by JD (2016-10-27 20:22:50)

Offline

#2 2016-10-28 12:06:48

turrican
Member
From: Barcelona
Registered: 2015-06-05
Posts: 94
Website

Re: Modeling 1 to many relationships

Hi JD,

Yes I am doing the same of you and works (but you need to be careful)... But the best way to do this (and more complex because mORMot needs a pivot table to establish relationship) is to use TSQLRecordMany implementation.

Last edited by turrican (2016-10-28 12:07:02)

Offline

#3 2016-10-28 13:43:50

JD
Member
Registered: 2015-08-20
Posts: 101

Re: Modeling 1 to many relationships

Hi turrican,

Thanks for your reply. I'll take note of your suggestion.

JD

Offline

Board footer

Powered by FluxBB