set book.Title=obj.Title ; from JSON obj ------->This line causes an OREF error I tried this initially, the Json object is a string eg "CACHE BOOK" and I want to save the RowId in the Title row in BOOK table when creating a new object in book.
do book.%Save()
Retrieving an existing ID I managed to work on that the problem or issue came to creating a new object in BOOK where I have to get RowId inserted into Title field given the obj.Title from postman is a string.
I have a table called TITLE which is generated form a class TITLE in RESTAPI folder so the package is RESTAPI.TITLE and I am creating a new object in another Book class but it has TITLE as a foreign key.
So if I want to insert data in the BOOK class the field for Title in the BOOK class is a foreign key in which is a primary key RowID in the TITLE class.
Given the new object I am creating is this {"ID":17, "Title":"LEARNING CAHE REST API"}
the title field in the BOOK class should give me the RowID which is 2
@Robert Cemper Thank you taht solves the problem for the OREF the challenge I find around it it doesn't post the data to the database the object I have can look like this
{"ID":17, "Title":"LEARNING CAHE REST API"}
I want it to store the following in the database
ID - > autoincrement - this I know how to do it. Title -> I want to store the ID where Value is "LEARNING CAHE REST API" in the All.Allbooks table
Thank you for all you help. it has open my eyes more. sorry if I ask the obvious questions I'm A C#/ASP.NET developer trying to learn Objectscript/CACHE/MUMPS
@Robert Cemper sorry for the confusion I'm trying to access the ID in Book class I used Title as a property name in Allbooks class.
In my structure of Dynamic object, I am trying to call content from a new JSON data to create a new Book title with the ID from Book class. I'm using Title as a foreign key property.
go to post
Thank you @Robert Cemper
You helped me much in finalizing the hardest part in my RESTAPI.
I tried too many things around creating a new object and I learnt a lot.
Thank you.
go to post
@Robert Cemper
Hope this is something better.
Class REST.BOOK Extends %Library.Persistent
{
Property Title As REST.TITLE;
Property Author As %String;
Storage Default
{
<Data name="BOOKDefaultData">
<Value name="1">
<Value>%%CLASSNAME</Value>
</Value>
<Value name="2">
<Value>Title</Value>
</Value>
<Value name="3">
<Value>Author</Value>
</Value>
</Data>
<DataLocation>^REST.BOOKD</DataLocation>
<DefaultData>BOOKDefaultData</DefaultData>
<IdLocation>^REST.BOOKD</IdLocation>
<IndexLocation>^REST.BOOKI</IndexLocation>
<StreamLocation>^REST.BOOKS</StreamLocation>
<Type>%Library.CacheStorage</Type>
}
}
Class RESTAPI.TITLE Extends %Library.Persistent
{
Index IDKeyIndex On RowId [ IdKey, PrimaryKey, Unique ];
Property RowId As %Library.String(COLLATION = "EXACT", TRUNCATE = 0) [ Required, SqlColumnNumber = 1, SqlFieldName = RowId ];
Property Title As %String;
Property Text As %String;
Storage Default
{
<Data name="TITLEDefaultData">
<Value name="1">
<Value>%%CLASSNAME</Value>
</Value>
<Value name="2">
<Value>Title</Value>
</Value>
<Value name="3">
<Value>Text</Value>
</Value>
</Data>
<DataLocation>^REST.TITLED</DataLocation>
<DefaultData>TITLEDefaultData</DefaultData>
<IdLocation>^REST.TITLED</IdLocation>
<IndexLocation>^REST.TITLEI</IndexLocation>
<StreamLocation>^REST.TITLES</StreamLocation>
<Type>%Library.CacheStorage</Type>
}
}
go to post
@Robert Cemper
set book=##class(BOOK).%New()
set book.RowId=obj.ID ; from JSON obj
set book.Title=obj.Title ; from JSON obj ------->This line causes an OREF error I tried this initially, the Json object is a string eg "CACHE BOOK" and I want to save the RowId in the Title row in BOOK table when creating a new object in book.
do book.%Save()
Retrieving an existing ID I managed to work on that the problem or issue came to creating a new object in BOOK where I have to get RowId inserted into Title field given the obj.Title from postman is a string.
go to post
@Robert Cemper
In the BOOK class I have
Index RowId On RowID [ IdKey, PrimaryKey, Unique ];
So when I have the Title of the Book I want to store the its RowID.
go to post
@Robert Cemper
RESTAPI.BOOK
The package is RESTAPI
Classes in the package are TITLE and BOOK
go to post
@Robert Cemper
I have a table called TITLE which is generated form a class TITLE in RESTAPI folder so the package is RESTAPI.TITLE and I am creating a new object in another Book class but it has TITLE as a foreign key.
So if I want to insert data in the BOOK class the field for Title in the BOOK class is a foreign key in which is a primary key RowID in the TITLE class.
Given the new object I am creating is this {"ID":17, "Title":"LEARNING CAHE REST API"}
the title field in the BOOK class should give me the RowID which is 2
so in the BOOK class the ID = 17 and Title = 2
That is what I'm trying to work out.
go to post
@Robert Cemper
I am trying to get the ID of Title in the TITLE table
{"ID":17, "Title":"LEARNING CAHE REST API"}
so if the obj.Title = "LEARNING CAHE REST API" I should get its RowId in the TITLE table
That is what I'm trying to solve
go to post
@Robert Cemper
Thank you taht solves the problem for the OREF the challenge I find around it it doesn't post the data to the database the object I have can look like this
{"ID":17, "Title":"LEARNING CAHE REST API"}
I want it to store the following in the database
ID - > autoincrement - this I know how to do it.
Title -> I want to store the ID where Value is "LEARNING CAHE REST API" in the All.Allbooks table
Thank you for all you help. it has open my eyes more. sorry if I ask the obvious questions I'm A C#/ASP.NET developer trying to learn Objectscript/CACHE/MUMPS
go to post
@Robert Cemper sorry for the confusion I'm trying to access the ID in Book class I used Title as a property name in Allbooks class.
In my structure of Dynamic object, I am trying to call content from a new JSON data to create a new Book title with the ID from Book class. I'm using Title as a foreign key property.
go to post
@Robert.Cemper1003 how do I go about creating a new Item given I want to save the ID from the Title class. I'm stuck at that.
SET obj = ##class(%DynamicObject).%FromJSON(%request.Content)
SET task = ##Class(All.Allbooks).%New()
SET task.Title = Obj.TitleID
DO task.%Save()
Thank you for your help
go to post
@Robert Cemper thank you this was helpful.