delphimvcframework/samples/master_details/BusinessObjects.pas

166 lines
4.3 KiB
ObjectPascal
Raw Normal View History

unit BusinessObjects;
// *************************************************************************** }
//
// Delphi MVC Framework
//
// Copyright (c) 2010-2021 Daniele Teti and the DMVCFramework Team
//
// https://github.com/danieleteti/delphimvcframework
//
// ***************************************************************************
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// ***************************************************************************
interface
uses
MVCFramework.Serializer.Commons,
MVCFramework.ActiveRecord,
System.Generics.Collections,
System.Classes;
type
[MVCNameCase(ncCamelCase)]
[MVCTable('articles')]
TArticles = class(TMVCActiveRecord)
private
[MVCTableField('id', [foPrimaryKey, foAutoGenerated])]
fID: Int64;
[MVCTableField('description')]
fDescription: String;
[MVCTableField('price')]
fPrice: Integer;
public
constructor Create; override;
destructor Destroy; override;
property ID: Int64 read fID write fID;
property Description: String read fDescription write fDescription;
property Price: Integer read fPrice write fPrice;
end;
[MVCNameCase(ncCamelCase)]
[MVCTable('order_details')]
TOrderDetail = class(TMVCActiveRecord)
private
[MVCTableField('id', [foPrimaryKey, foAutoGenerated])]
fID: Int64;
[MVCTableField('id_order')]
fIDOrder: Int64;
[MVCTableField('id_article')]
fIDArticle: Int64;
[MVCTableField('unit_price')]
fUnitPrice: Currency;
[MVCTableField('discount')]
fDiscount: Integer;
[MVCTableField('quantity')]
fQuantity: Integer;
[MVCTableField('description')]
fDescription: String;
[MVCTableField('total')]
fTotal: Currency;
public
constructor Create; override;
destructor Destroy; override;
property ID: Int64 read fID write fID;
property IDOrder: Int64 read fIDOrder write fIDOrder;
property IDArticle: Int64 read fIDArticle write fIDArticle;
property UnitPrice: Currency read fUnitPrice write fUnitPrice;
property Discount: Integer read fDiscount write fDiscount;
property Quantity: Integer read fQuantity write fQuantity;
property Description: String read fDescription write fDescription;
property Total: Currency read fTotal write fTotal;
end;
[MVCNameCase(ncCamelCase)]
[MVCTable('orders')]
TOrder = class(TMVCActiveRecord)
private
[MVCTableField('id', [foPrimaryKey, foAutoGenerated])]
fID: Int64;
[MVCTableField('id_customer')]
fIDCustomer: Integer;
[MVCTableField('order_date')]
fOrderDate: TDate;
[MVCTableField('total')]
fTotal: Currency;
fDetails: TObjectList<TOrderDetail>;
protected
procedure OnAfterLoad; override;
public
constructor Create; override;
destructor Destroy; override;
property ID: Int64 read fID write fID;
property IDCustomer: Integer read fIDCustomer write fIDCustomer;
property OrderDate: TDate read fOrderDate write fOrderDate;
property Total: Currency read fTotal write fTotal;
property OrderItems: TObjectList<TOrderDetail> read fDetails;
end;
implementation
uses
System.SysUtils;
constructor TArticles.Create;
begin
inherited Create;
end;
destructor TArticles.Destroy;
begin
inherited;
end;
constructor TOrderDetail.Create;
begin
inherited Create;
end;
destructor TOrderDetail.Destroy;
begin
inherited;
end;
constructor TOrder.Create;
begin
inherited Create;
fDetails := TObjectList<TOrderDetail>.Create(true);
end;
destructor TOrder.Destroy;
begin
fDetails.Free;
inherited;
end;
procedure TOrder.OnAfterLoad;
var
lList: TObjectList<TOrderDetail>;
begin
inherited;
lList := TMVCActiveRecord.SelectRQL<TOrderDetail>(Format('eq(order_id,%d)',[ID]), 1000);
try
fDetails.Clear;
fDetails.AddRange(lList);
lList.OwnsObjects := False;
finally
lList.Free;
end;
end;
end.