You are not logged in.
Pages: 1
hi ab,
i have this tsqlrecord class
TSQLAssemblea = class (TSQLRecord)
private
FComune: TSQLComune;
FOraInizio: TTime;
FOraFine: TTime;
FPresenze: TSQLPresenzeAssemblea;
FOrdineDelGiorno: RawUTF8;
FIndirizzo: RawUTF8;
FCondominio: TSQLCondominio;
FData: TDateTime;
FUltimaModifica: TModTime;
FDataCreazione: TCreateTime;
public
function getMillesimiPresenti: Integer;
function getCollection(aClient: TSQLRest): TPresenze;
procedure setCollection(aClient: TSQLRest; aColl: TPresenze);
published
property Data: TDateTime read FData write FData;
property OraInizio: TTime read FOraInizio write FOraInizio;
property OraFine: TTime read FOraFine write FOraFine;
property Indirizzo: RawUTF8 read FIndirizzo write FIndirizzo;
property Comune: TSQLComune read FComune write FComune;
property Condominio: TSQLCondominio read FCondominio write FCondominio;
property OrdineDelGiorno: RawUTF8 read FOrdineDelGiorno write FOrdineDelGiorno;
property Presenze: TSQLPresenzeAssemblea read FPresenze write FPresenze;
//property MillesimiPresenti: Integer read getMillesimiPresenti;
property DataCreazione: TCreateTime read FDataCreazione write FDataCreazione;
property UltimaModifica: TModTime read FUltimaModifica write FUltimaModifica;
end;
i istantiate a tsqlAssemblea object with
Assemblea := TSQLAssemblea.Create(ControllerMain.Client, IDAssemblea);
now i istantiate a tsqlCondominio object with
Assemblea.Condominio := TSQLCondominio.Create(ControllerMain.Client, Assemblea.Condominio.ID);
because in the orm i have just the ID (sharding), and it's ok for me.
now i would modify some property of Assemblea object like this
Assemblea.OraFine := Time();
and then i would save the change with
ControllerMain.Client.Update(Assemblea);
and i aspect its right instead i loss Assemblea.Condominio.
i must to do before the update
Assemblea.Condominio := TSQLCondominio(Assemblea.Condominio.ID);
but why if i don't modify Condominio?
thanks,
Emanuele.
Offline
All sub fields should NOT contain a real instance of the class.
For instance, writing
Assemblea.Condominio := TSQLCondominio.Create(ControllerMain.Client, Assemblea.Condominio.ID);
is incorrect and will break the ORM.
Use a temporary TSQLCondominio record, as stated by the documentation.
Download the latest version - the framework has just been updated to 1.16 - and check pages 67 to 72 for the use of the pivot table, and more generally page 56 to 58.
See page 57:
When accessing the detail objects, you should not access directly to FirstOne or SecondOne properties (there are not class instances, but IDs), then use instead the TSQLRecord. Create(aClient: TSQLRest; aPublishedRecord: TSQLRecord: ForUpdate: boolean=false) overloaded constructor, as such:
var One: TSQLMyFileInfo;
MyFile: TSQLMyFile;
begin
MyFile := TSQLMyFile.Create(Client,aMyFileID);
try
// here MyFile.FirstOne.MyFileDate will trigger an access violation
One := TSQLMyFileInfo.Create(Client,MyFile.FirstOne);
try
// here you can access One.MyFileDate or One.MyFileSize
finally
One.Free;
end;
finally
MyFile.Free;
end;
end;
Or with a with statement:
with TSQLMyFileInfo.Create(Client,MyFile.FirstOne) do
try
// here you can access MyFileDate or MyFileSize
finally
Free;
end;
Mapping a TSQLRecord field into an integer ID is a bit difficult to learn at first. It was the only way we found out in order to define a "one to one" or "one to many" relationship within the class definition, without any property attribute features of the Delphi compiler (only introduced in newer versions). The main drawback is that the compiler won't be able to identify at compile time some potential GPF issues at run time. This is up to the developper to write correct code, when dealing with TSQLRecord properties.
Offline
Pages: 1