How to map a custom boolean property?

How do i map a boolean property which is stored as an smallint in database?

For example:

TUser = class
strict private
  ...
  FActive: Boolean;
public
  ...
  property Active: Boolean read FActive write FActive;
end;


How to map the "Active" property so that value "True" is stored as "1" and value "False" is stored as "0" in database?

Regards

Create one integer field that will be mapped to the database, and then for OOP usage create the boolean property that wraps the integer value:



TUser = class
  [Column('Active')]
  FActiveInt: Integer;
 public
  property Active: Boolean read GetActive write SetActive;


function TUser.GetActive: Boolean;
begin
  Result := FActiveInt <> 0;
end


procedure TUser.SetActive(const Value: Boolean);
begin
  if Value then 
    FActiveInt := 1
  else
    FActiveInt := 0;
end;

Great!

I just ecountered this problem on an existing DB and this is a time-saver! :)

1 Like

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.