You are not logged in.
Pages: 1
Just trying sample 1, try to change the get of a property to see how it behaves.
TSQLSampleRecord = class(TSQLRecord)
private
fQuestion: RawUTF8;
fName: RawUTF8;
fTime: TModTime;
function GetQuestion: RawUTF8;
published
property Time: TModTime read fTime write fTime;
property Name: RawUTF8 read fName write fName;
//property Question: RawUTF8 read fQuestion write fQuestion;
property Question: RawUTF8 read GetQuestion write fQuestion;
end;
function TSQLSampleRecord.GetQuestion: RawUTF8;
begin
result := 'Msg: '+ fQuestion;
end;
i put this test "test message" and click add the message.
When i retrieve the message this is the result:
Result seems called 3 times!
"Msg: Msg: Msg: test message"
What im doing wrong?
Offline
This code is plenty wrong, I'm afraid.
You are changing the way a property is expected to work.
Input / output should be equipotent.
During the JSON serialization and data transmission within the ORM, your getter method breaks this expectation, so the content is changed.
Online
IMHO this is better aproach.
TSQLSampleRecord = class(TSQLRecord)
private
fQuestion: RawUTF8;
fName: RawUTF8;
fTime: TModTime;
public
function GetQuestion: RawUTF8;
published
property Time: TModTime read fTime write fTime;
property Name: RawUTF8 read fName write fName;
property Question: RawUTF8 read fQuestion write fQuestion;
end;
function TSQLSampleRecord.GetQuestion: RawUTF8;
begin
result := 'Msg: '+ fQuestion;
end;
Last edited by turrican (2016-12-29 09:15:13)
Offline
Can please explain why? Is some internal need for TSQLRecord? fGetQuestion is not ever a function for that class. Don't assorted to the property either.
If i think in delphi/pascal terms, my solution is ok.
Last edited by donaldshimoda (2016-12-28 20:45:32)
Offline
It breaks the whole serialization process.
As such it is not OK. ?
Ok, then is not possible to use standard property methods? like in standard Delphi classes?
Offline
If you use them properly, yes of course you can.
Can you please explain me how has to be used, using my code and showing what is wrong there and which will be the correct usage?
Offline
Getter / Setter methods of published properties should be equipotent, to let serialization work as expected:
...
property Question: RawUTF8 read GetQuestion write SetQuestion;
end;
function TSQLSampleRecord.GetQuestion: RawUTF8;
begin
result := 'Msg: '+ fQuestion;
end;
procedure TSQLSampleRecord.SetQuestion(const value: RawUTF8);
begin
if copy(value,1,5)='Msg: ' then
fQuestion := copy(value,6,maxInt);
end;
So here fQuestion will be filled back with the expected value.
If you need to change on the fly a property value for representation purposes (e.g. return a date as text), use a function or a public (not published) property.
Online
Getter / Setter methods of published properties should be equipotent, to let serialization work as expected:
... property Question: RawUTF8 read GetQuestion write SetQuestion; end; function TSQLSampleRecord.GetQuestion: RawUTF8; begin result := 'Msg: '+ fQuestion; end; procedure TSQLSampleRecord.SetQuestion(const value: RawUTF8); begin if copy(value,1,5)='Msg: ' then fQuestion := copy(value,6,maxInt); end;
So here fQuestion will be filled back with the expected value.
If you need to change on the fly a property value for representation purposes (e.g. return a date as text), use a function or a public (not published) property.
Sorry, it seems like a freak hack to the normal behavior you expect from a object. That answer my question: you cannot use getter and setter on the tsqlrecords. Calling a function to change a value will by *by hand* not automatic when you get or set the property, as work in normal OOP.
Best regards.
Offline
I'm sorry, but you are wrong.
You can use getters and setters on TSQLRecord.
For published properties, you have to ensure that getter and setter are equipotent.
And it is not tied to TSQLRecord or mORMot.
It is mandatory for ANY persistence mechanism, even for Delphi TPersistent streaming.
Online
Pages: 1