Hello,
First of all I would like to thank all the support that I have been having from the community.
Thanks!
Now I have a problem using the RTTI function setValue.
In a function I receive a TObject and an ID. I query the database and with RTTI I fill in the TOject properties.
The procedure is executed without any error.
However, when accessing the properties of this TObject, the values are filled in incorrectly. The value of one property ends up being repeated in others, with no logic in the structure.
Here is the content of the function:
var
Contexto: TRttiContext;
TypObj: TRttiType;
Prop: TRttiProperty;
strInsert, strFields: String;
Atributo: TCustomAttribute;
PK: string;
q: TZQuery;
begin
strInsert := '';
strFields := '';
PK := getFieldPKRTTI(Obj.ClassType);
strInsert := 'SELECT * FROM ' + getTableNameRTTI(Obj.ClassType) +
' WHERE ' + PK + ' = ' + ID.ToString ;
q := Conexao.getQuery(strInsert);
q.Open;
Contexto := TRttiContext.Create;
TypObj := Contexto.GetType(Obj.ClassInfo);
for Prop in TypObj.GetProperties do begin
for Atributo in Prop.GetAttributes do begin
t := 0;
if Atributo is TIntegerCampo then
begin
t := q.FieldByName(TIntegerCampo(Atributo).NomeCampo).AsInteger;
Prop.SetValue(PByte(Obj),t);
end;
if Atributo is TOutroCampo then
begin
case TOutroCampo(Atributo).TipoCampo of
ftBoolean:
begin
t := q.FieldByName(TOutroCampo(Atributo).NomeCampo).AsString;
Prop.SetValue(PByte(Obj),t);
end;
ftBlob, ftString:
begin
t := q.FieldByName(TOutroCampo(Atributo).NomeCampo).AsString;
Prop.SetValue(PByte(Obj),t);
end;
ftFloat, ftCurrency:
begin
t := q.FieldByName(TOutroCampo(Atributo).NomeCampo).AsFloat;
Prop.SetValue(PByte(Obj),t);
end;
ftDate:
begin
t := q.FieldByName(TOutroCampo(Atributo).NomeCampo).AsDateTime;
Prop.SetValue(PByte(Obj),t);
end;
ftTime:
begin
t := q.FieldByName(TOutroCampo(Atributo).NomeCampo).AsDateTime;
Prop.SetValue(PByte(Obj),t);
end;
end;
end;
end;
end;
end;
When I access the properties of a TObject I would see it like this:
ID_PESSOA = 1
ID_EMPRESA = 2
ID_UnidNeg = 3
NOME = TESTE
FANTASIA = TESTE FANTASIA
ENDERECO = RUA TESTE
NUMERO = 111
BAIRRO = BAIRRO TESTE
CIDADE = CIDADE TESTE
ESTADO = SP
LIMITE = 1.5
LIMTE CREDITO = 1.99
DATA CADASTRO = 25-1-20
HORA CADASTRO = 15:15:00
ATIVO = f
BLOB = fdsfsdfsfds
LIMITE CREDITO = 1.99
The result is this:
ID_PESSOA = 1
ID_EMPRESA = 2
ID_UnidNeg = 3
NOME = fdsfsdfsfds
FANTASIA = f
ENDERECO = fdsfsdfsfds
NUMERO = f
BAIRRO = fdsfsdfsfds
CIDADE = f
ESTADO = fdsfsdfsfds
LIMITE = 1.5
LIMTE CREDITO = 1.99
DATA CADASTRO = 25-1-20
HORA CADASTRO = 15:15:00
ATIVO = f
BLOB = 15:15:00
LIMITE CREDITO = 1.99
Is this a problem with RTTI or my logic?
Any help will be welcome.
Thanks!