Day 2: Developing with InterSystems Objects and SQL
I'm participating in the Developing with InterSystems Objects and SQL with Joel Solon. The course is very nice and I will share with you some tips I got during the training. Tips presented in the day 2:
- You can create persistent classes (classes with a correspondent table in the database to persist class properties).
- An Persistent class example:
Class dc.Person extends (%Persistent){Property Name As %String;Property BirthDate As %Date;}
- When you extends %Persistent, you get %New() to create a new instance in the memory, %Save() to save in the database and %Id() to get the unique ID to the instance in the Database and %OpenId() to load the instance with database values.
- Persistent classes allow you calls %Deleteid() to delete an instance from the database, %DeleteExtent() to delete all saved objects (delete without where!) and %ValidateObject() to validate data passed before save (validate required, sizes, etc).
- Persistent classes have %IsModified() to check with the data changed in the memory (see joel tip in the comments) and %Reload() to get these changes.
- To get possible errors when you try to %Save() or %Delete(): set status = person.%Save(), write status. If success in save will return 1.
- We can use do $system.Status.DisplayError(status) to see error details.
- To call persistent class methods do: ##class(dc.Person).%Save().
- To call a persistent instance method do: ..Method(). It's the same to refer a properties, do: write ..Name.
- To remove an object ou variable from program or terminal memory use kill person or set person = "". If you use kill only, all references wiil be removed from memory (not from database, to database use killextent).
- If you want a utility method to populate test data, extend your persistent class with %Populate and call Populate(number of rows).
- You can create embedded classes to a persistent class extendind %SerialObject (a persistent class without Id, because is linked with a persistent class). Example:
Class PackageName.ClassName Extends %SerialObject{Property Phone As %String;Property Email As %String;}
- This serial will be a property into your persistent class:
Class dc.Person extends (%Persistent){Property Name As %String;Property BirthDate As %Date;Property Contact As dc.Contact;}
- In IRIS Database will be create only one table with Person and Contact properties.
- You can create Indexes to get uniqueness or to tunning queries. Example: Index NameIndex On Name [Unique].
- If you create an index you need to build the index in the Management Portal if table is not empty.
- To create a constructor method override %OnNew(). This is a callback method called when call %New(). There are other callback methods.
- IRIS has a great support to JSON. You can load a JSON to a object calling set name = {}.%FromJSON("{""Name"":""Yuri""}").
- You can write a JSON from the object executing: name.%ToJSON().
- To JSON arrays exists in the IRIS and Caché (thanks @Robert cc.Cemper alert), but only in IRIS we have fomatter and zwrite to JSON.
Tomorrow I will publish the day 3.
PS.: this is a resume, but there are more content learned in the course.