Written by

Question Pedro Lopes · Aug 9, 2023

What is the correct way to file the ID of a relationship?

Class Contatos.Amiguinho Extends %Persistent
{
Property Moradia As Cidade;

Relationship Trabalho As Contatos.Empresa [ Cardinality = one, Inverse = Nomedaempresa ];
}

------------------------Routine-----------------------------------
Set objcontato=##class(Contatos.Amiguinho).%New()
Set IDm=3,IDt=2
Set objcontato.Moradia=##class(Contatos.Cidade).%OpenId(IDm)    ;<-- it works for "Property Moradia As Cidade"
Set objcontato.Trabalho=##class(Contatos.Empresa).%OpenId(IDt)  ;<-- it doesn't works for "Relationship Trabalho As Contatos.Empresa"
Set ret=objcontato.%Save()

------------Question-----------------------------

What's the right way to do for "Relationship Trabalho As Contatos.Empresa"?
 

Comments

Pedro Lopes  Aug 9, 2023 to Luis Angel Pérez Ramos

<INVALID OREF>

0
Pedro Lopes  Aug 9, 2023 to Luis Angel Pérez Ramos

<METHOD DOES NOT EXIST>

0
Robert Cemper · Aug 9, 2023

Instead of Set  
Setobjcontato.Trabalho=##class(Contatos.Empresa).%OpenId(IDt)  
you need to use method Insert()
Do objcontato.Trabalho.Insert(##class(Contatos.Empresa).%OpenId(IDt) 

See Docs

0
Pedro Lopes  Aug 9, 2023 to Robert Cemper

Do objcontato.Trabalho.Insert(##class(Contatos.Empresa).%OpenId(IDt))
 

<INVALID OREF>

0
Robert Cemper  Aug 9, 2023 to Pedro Lopes

You just can insert existing IDs.
So you have to check first:

set emp=##class(Contatos.Empresa).%OpenId(IDt)
if $isObject(empDo objcontato.Trabalho.Insert(emp)

0
Robert Cemper  Aug 9, 2023 to Robert Cemper

sorry, Insert() is only for the "MANY" side of the Relationship

0
Pedro Lopes  Aug 9, 2023 to Robert Cemper

 s emp=##class(Contatos.Empresa).%OpenId(IDt) If $isObject(emp) Do objcontato.Tr
                                                                ^
abalho.Insert(emp)
<INVALID OREF>C+5^cont
USER 2d0>zw
 
IDt=1
NInd="NomeIndex"
emp=<OBJECT REFERENCE>[2@Contatos.Empresa]
objcontato=<OBJECT REFERENCE>[1@Contatos.Amiguinho]
pakobj="Contatos.Amiguinho"

0
Pedro Lopes  Aug 9, 2023 to Luis Angel Pérez Ramos

There is only one Property at Contatos.Empresa

Property Nomedaempresa As %String;

0
Robert Cemper  Aug 9, 2023 to Pedro Lopes

It also needs an ID and a Relationship.

0
Robert Cemper  Aug 9, 2023 to Pedro Lopes

According to class Class Contatos.Amiguinho you presented
Class Contatos.Empresa should look similar to this to work:

Class Contatos.Empresa Extends%Persistent
{
Relationship Nomedaempresa As 
    Contatos.Amiguinho [ Cardinality = many, Inverse = Trabalho ];Property Nome As%String;

Storage Default
}
0
Vitaliy Serdtsev · Aug 10, 2023

Try this:

 <FONT COLOR="#0000ff">Set </FONT><FONT COLOR="#800000">objcontato</FONT><FONT COLOR="#000000">=</FONT><FONT COLOR="#000080">##class</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#008080">Contatos.Amiguinho</FONT><FONT COLOR="#000000">).</FONT><FONT COLOR="#0000ff">%New</FONT><FONT COLOR="#000000">()
 </FONT><FONT COLOR="#0000ff">Do </FONT><FONT COLOR="#800000">objcontato</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">MoradiaSetObjectId</FONT><FONT COLOR="#000000">(3)
 </FONT><FONT COLOR="#0000ff">Do </FONT><FONT COLOR="#800000">objcontato</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">TrabalhoSetObjectId</FONT><FONT COLOR="#000000">(2)
 </FONT><FONT COLOR="#0000ff">Set </FONT><FONT COLOR="#800000">ret</FONT><FONT COLOR="#000000">=</FONT><FONT COLOR="#800000">objcontato</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">%Save</FONT><FONT COLOR="#000000">()</FONT>
See Fastest Way to Connect Objects
0
Pedro Lopes  Aug 10, 2023 to Vitaliy Serdtsev

Perfect!!! Thanks a lot.

0