Advertisement
danieleteti

DMVCFramework - ActiveRecord - Transient Fields

Feb 22nd, 2019
598
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.69 KB | None | 0 0
  1. type
  2.   [MVCNameCase(ncLowerCase)]
  3.   [MVCTable('customers')]
  4.   TCustomerWithTransient = class(TMVCActiveRecord)
  5.   private
  6.     [MVCPrimaryKey('id', [foAutoGenerated])]
  7.     fID: Integer;
  8.     [MVCTableField('code', [foTransient])]
  9.     fCode: string;
  10.     [MVCTableField('description')]
  11.     fCompanyName: string;
  12.     [MVCTableField('city', [foTransient])]
  13.     fCity: string;
  14.   public
  15.     property ID: Integer read fID write fID;
  16.     property Code: string read fCode write fCode;
  17.     property CompanyName: string read fCompanyName write fCompanyName;
  18.     property City: string read fCity write fCity;
  19.   end;
  20.  
  21. ...
  22.  
  23. procedure TMainForm.btnTransientFieldsClick(Sender: TObject);
  24. var
  25.   lCustomer: TCustomerWithTransient;
  26.   lID: Integer;
  27. begin
  28.   Log('Transient CRUD test');
  29.   lCustomer := TCustomerWithTransient.Create;
  30.   try
  31.     {
  32.       'Code' and City will not be persisted because defined as 'transient'
  33.     }
  34.     lCustomer.Code := '1234';
  35.     lCustomer.CompanyName := 'Google Inc.';
  36.     lCustomer.City := 'Montain View, CA';
  37.     lCustomer.Insert;
  38.     lID := lCustomer.ID;
  39.     Log('Just inserted "transient" Customer ' + lID.ToString);
  40.   finally
  41.     lCustomer.Free;
  42.   end;
  43.  
  44.   lCustomer := TMVCActiveRecord.GetByPK<TCustomerWithTransient>(lID);
  45.   try
  46.     lCustomer.CompanyName := lCustomer.CompanyName + ' changed!';
  47.     lCustomer.Code := 'this code will not be saved';
  48.     lCustomer.Update;
  49.     Log('Just updated Customer ' + lID.ToString);
  50.   finally
  51.     lCustomer.Free;
  52.   end;
  53.  
  54.   lCustomer := TMVCActiveRecord.GetByPK<TCustomerWithTransient>(lID);
  55.   try
  56.     lCustomer.Delete;
  57.     Log('Just deleted "transient" Customer ' + lID.ToString);
  58.   finally
  59.     lCustomer.Free;
  60.   end;
  61. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement