Note: This post if part of a complete series on the West Wind wwBusiness library.
Click here to see the full index
Before we start digging into parent/child data access with wwBusinessPro, let’s have a review of the basics. Suppose we want a Invoice object, and child set of Invoice Line Items that belong to that invoice. This is possible, even simple, with wwBusninessPro. In fact, in this post I’ll also show you how to handle a second child collection, like Invoice Payments, related to the Invoice. If you follow the example shown here, you’ll be able to handle cases where you have a single business objust with MULTIPLE child collections.
For example, here is what a really complex “aggregate” business object can look like. In my main business app, I have one complex business object that loads 4 additional child cursors in addition to the base oLineItems collection, and, it also loads customer and user info into oCustomer and oUser objects that I added onto the BO.
So let’s get started…
Parent and One Child Cursor
Parent class – Create your Invoice class as subclass of wwBusProParent.
At this point, you will see that it has *ONE* child collection object and related properties already built in. The oLineItems object will hold a object reference to the child item class. To make it active (so that it fetches child records every time you fetch a new parent object), you will need to set the cLineItemsClass, cLineItemsAlias, and lLoadtems properties.
The cLineItemsClassproperty points to a lineitems class that you must define in your VCX and it holds the table name of where the child records live and the key field name which links it to the PK on the parent.
The cLineItemsAliasproperty is the name of the cursor that where the child records will live.
Finally, be sure you set the lLoadLineItems property to .t. to make sure the main BO will load the line items each time you call the Get() method on the Parent.
Child Class – Create your InvoiceLineItems child class as a subclass of wwBusProItemList.
Set the properties cFilename and cFKField, which identify the Child table, and the field in the table that links back to the Parent PK in the invoice class.
So that’s it. You’ve just set up the built-in functionality for the wwBusinessProParent class to handle the Parent BO and its set of child objects.
Need to handle MULTIPLE sets of child records?
Let’s say you also want to fetch all InvoicePayments records every time you load up an Invoice object…
After you’ve configured your “Invoice” and “InvoiceLineItems” classes as described above, and add the following properties and one method to cover the “Payments” collection:
- lAllowEditPaymentItems (optional, but something I include on my so I can enable or disable stuff in the UI layer)
Now you need write two simple code blocks:
1. To populate the PaymentItems cursor (in addition to the LineItems cursor) every time you fetch an Invoice, you need to override the LoadRelatedData() method on the Invoice class like this:
2. Define the GetPaymentItems() method like this:
Once you have brought this Invoice object to life, you will have 3 classes in your VCX:
Invoice (based on wwBusProParent)
InvoiceLineItems (based on wwBusProItemList) (this is what the default oLineItems will point to)
InvoicePaymentItems (based on wwBusProItemList) (this is what the above new properties and methods will point to)
When complete, your “Aggregate” Invoice Business Object will look like this:
I know it might seem complicated at first, but once you understand what’s happening, it really not hard.