MVC master-detail with AddFields and AddGrid
MPFormModel():New(); AddFields(); AddGrid(); SetRelation()Defines master/detail with explicit IDs and relation.
Overview
AddFields creates the first Field submodel as master; AddGrid creates detail owned by that ID. SetRelation defines child/master linkage and ordering. The community example uses SBM/SB1; this adaptation uses fictitious ZZ1/ZZ2 tables to avoid suggesting standard-register changes. The child branch comes from xFilial("ZZ2") because sharing can differ from the master. Create and verify the schema/index in staging before evaluating the block. (Sources: refs. 1–5.)
Syntax
MPFormModel():New(); AddFields(); AddGrid(); SetRelation()Commented example
#Include "Protheus.ch"
#Include "FWMVCDef.ch"
User Function Ed554()
// Requires test dictionary ZZ1/ZZ2.
Local bBuild := {|| BuildModel()}
ConOut(ValType(bBuild))
Return Nil
Static Function BuildModel()
Local oModel := MPFormModel():New("ED554M")
Local oStruct := FWFormStruct(1,"ZZ1")
oModel:AddFields("ZZ1MASTER",Nil,oStruct)
oModel:AddGrid("ZZ2DETAIL","ZZ1MASTER",FWFormStruct(1,"ZZ2"))
oModel:SetRelation("ZZ2DETAIL",{{"ZZ2_FILIAL",'xFilial("ZZ2")'},;
{"ZZ2_COD","ZZ1_COD"}},"ZZ2_FILIAL+ZZ2_COD+ZZ2_ITEM")
oModel:SetPrimaryKey({"ZZ1_FILIAL","ZZ1_COD"})
Return oModelPrints B; the helper is not evaluated and creates/writes no tables. Under review: editorial example neither compiled nor run in Protheus. Check includes, release, RDD and context before testing.
- Verify field types/widths, primary key, relation and actual index before activation. Test validation, cancellation and persistence separately; the View must bind the correct submodel IDs.
- Under review: editorial example neither compiled nor run in Protheus. Check includes, release, RDD and context before testing.

Comments
There are no approved comments yet.
Sign in with Google or Microsoft to comment.