Skip to main content

Populating Again

We have already seen the Caché population utility in action. We populated Contact twice: first without Address and then with Address. In both cases we relied completely on the default behavior of the utility to generate appropriate data for the different properties in our class. The default behavior worked well. It generated names for the Name property, city names for the City property, street addresses for the Street property, and so on. It also randomly selected either “Business” or “Personal” for Type, which we restricted to these two possible values. Now, however, the default behavior will not give us exactly what we want. PhoneNumber contains a property called Number and the default behavior of the populate utility for a property named Number is to generate a random number, but not a random phone number.

The populate utility does, however, include a class, %PopulateUtilsOpens in a new tab, which contains methods for generating the right sorts of data for a number of different situations. In particular, it contains the USPhone method for generating US style phone numbers. We can link a %PopulateUtilsOpens in a new tab method to a property through the property's POPSPEC parameter.

In PhoneNumber, update the property declaration of Number to include a POPSPEC parameter with the value “USPhone()”. It looks like this.

Property Number As %String (POPSPEC="USPhone()");

After updating the class definition, save and recompile the classes.

Now, use the Terminal to remove all instances of Contact. Use the following command:

USER>Do ##class(ContactDB.Contact).%KillExtent()

Next, populate Contact and then PhoneNumber. Use the following commands:

USER>Do ##class(ContactDB.Contact).Populate(10)
USER>Do ##class(ContactDB.PhoneNumber).Populate(25)


You can also create your own method for populating a property. Again, you link the property to the method through POPSPEC. For more information, read The POPSPEC Parameter in The Caché Populate Utility section of Using Caché Objects.

FeedbackOpens in a new tab