Usina BRAdvPL Guide
Sign in
← All topics
PRACTICAL GUIDEUnder review

Relationships and primary keys in MVC

Distinguishes model identity from submodel relationships.

Relationships and primary keys in MVCAdvPL
01 · OVERVIEW

Overview

SetPrimaryKey defines the first FormField identity, not one key per grid. The framework can use an SX2-declared key. SetRelation defines detail loading/foreign-key linkage to its owner, including detail order. Use the child table xFilial because parent/child sharing can differ. This uninvoked configuration uses hypothetical ZZ1/ZZ2 fields and indexes that must exist in the dictionary. Configure before Activate and test insert, update, copy and delete. (Sources: refs. 1–2.)

03 · PRACTICAL EXAMPLE

Commented example

#Include "Protheus.ch"

User Function Ed615()
    // ZZ1/ZZ2 model already defined but not active; blocks not invoked.
    Local bKey := {|oModel| oModel:SetPrimaryKey({"ZZ1_FILIAL","ZZ1_COD"})}
    Local bRelation := {|oModel| oModel:SetRelation("ZZ2DETAIL", ;
        {{"ZZ2_FILIAL",'xFilial("ZZ2")'}, {"ZZ2_COD","ZZ1_COD"}}, ;
        "ZZ2_FILIAL+ZZ2_COD+ZZ2_ITEM")}
    ConOut(ValType(bKey)+ValType(bRelation))
Return Nil
Expected result

Creates two blocks, BB; neither creates tables nor configures a real model. Under review: editorial example neither compiled nor run in Protheus. Check includes, release, RDD and context before testing.

BEST PRACTICES
  • Verify IDs, expressions, types, duplicates and integrity. The parent key is not the complete detail order.
  • Under review: editorial example neither compiled nor run in Protheus. Check includes, release, RDD and context before testing.

Related content

REFERENCES
  1. TOTVS. FWFormModel. Seções SetRelation, SetPrimaryKey, SetOperation, VldData, CommitData e GetErrorMessage. Accessed on 2026-10-27.
  2. Atilio. dan-atilio/AdvPL. Exemplos/MVC/zMVCMd3.prw. Arquivo completo consultado; adaptação editorial. Accessed on 2026-10-27.
Status
Under review
Page created on
Last reviewed on
Original language
Portuguese
Reviewed by
Usina.BR
0 approved comment(s)

Comments

There are no approved comments yet.

Sign in with Google or Microsoft to comment.

Powered by Usina Docs · Alpha