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
What error do you get?
<INVALID OREF>
<METHOD DOES NOT EXIST>
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))
Do objcontato.Trabalho.Insert(##class(Contatos.Empresa).%OpenId(IDt))
<INVALID OREF>
You just can insert existing IDs.
So you have to check first:
set emp=##class(Contatos.Empresa).%OpenId(IDt)
if $isObject(emp) Do objcontato.Trabalho.Insert(emp)
sorry, Insert() is only for the "MANY" side of the Relationship
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"
Could you paste here Contatos.Empresa properties?
There is only one Property at Contatos.Empresa
Property Nomedaempresa As %String;
Does it extend %Persistent?
It also needs an ID and a Relationship.
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
}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
Perfect!!! Thanks a lot.