· Feb 24, 2021 3m read

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:

  1. You can create persistent classes (classes with a correspondent table in the database to persist class properties).
  2. An Persistent class example:
    Class dc.Person extends (%Persistent)
        Property Name As %String;
        Property BirthDate As %Date;
  3. 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.
  4. 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).
  5. 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.
  6. To get possible errors when you try to %Save() or %Delete(): set status = person.%Save(), write status. If success in save will return 1.
  7. We can use do $system.Status.DisplayError(status) to see error details.
  8. To call persistent class methods do: ##class(dc.Person).%Save().
  9. To call a persistent instance method do: ..Method(). It's the same to refer a properties, do: write ..Name.
  10. 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).
  11. If you want a utility method to populate test data, extend your persistent class with %Populate and call Populate(number of rows).
  12. 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;
  13. 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;
  14. In IRIS Database will be create only one table with Person and Contact properties.
  15. You can create Indexes to get uniqueness or to tunning queries. Example: Index NameIndex On Name [Unique].
  16. If you create an index you need to build the index in the Management Portal if table is not empty.
  17. To create a constructor method override %OnNew(). This is a callback method called when call %New(). There are other callback methods.
  18. IRIS has a great support to JSON. You can load a JSON to a object calling set name = {}.%FromJSON("{""Name"":""Yuri""}").
  19. You can write a JSON from the object executing: name.%ToJSON().
  20. To JSON arrays exists in the IRIS and Caché (thanks @rcc 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.

Discussion (4)3
Log in or sign up to continue