Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

How to define child objects
xomega
#1 Posted : Friday, August 3, 2012 6:13:31 PM(UTC)
xomega



A child object (also called sub object) is an object that is considered a part of its parent object, cannot exist without a parent and should generally not be referenced outside of the parent object. For example, an invoice may contain a list of line items, which are sub-objects of the invoice object.

Defining a child object is similar to defining a primary object. The major difference is that the parent's unique key is always implicitly included in the list of child object's fields, which automatically establishes a relationship between the child object and the parent object. If the child's key field is serial, then it will be considered its unique key. Otherwise the child's unique key will include its key field and the unique key of its parent. The child's unique key will in turn be implicitly included in all the grand child objects and may also be part of their own unique keys respectively.

Here are the steps for defining a child object.
  1. Add subobjects as the last element to the parent object if it doesn't exist.
  2. Add an object element to the subobjects group element.
  3. Set the name attribute to the name of the child object. Note that the full name of the object will be built from this name appended to the full name of its parent object.
  4. Define a single key field or a composite key for the child object. If this is a serial single key, then it will be the primary key of the child object. Otherwise this key will be used to identify the child object among other child objects of the same type and therefore the primary key will be composed from this key and the primary key of the parent.
  5. Add other non-key fields to the object the same way as when defining a primary object.
  6. If you want a better control over the database foreign key generated from the implicit relationship between the child object in the parent then you can add a config > sql:parent-foreign-key child element to the object element of the child object, where the sql prefix should map to the namespace http://www.xomega.net/sql.
  7. If you want to override the name of the foreign key generated for the database, then you can set the name attribute accordingly.
  8. If you want to override the update or delete actions, then you can set the corresponding attributes as appropriate.
  9. You can also specify to NOT generate the foreign key by setting the mode attribute to none.
  10. Add the doc > summary element to provide a description for your child object.

The following sample demonstrates such a setup.
Code:
<module xmlns="http://www.xomega.net/omodel">
  <types>
    <type name="invoice number" base="integer"/>
  </types>
  <objects>
    <object name="invoice">
      <fields>
        <field name="number" type="invoice number" key="serial" required="true"/>
        <field name="customer" type="customer"/>
      </fields>
      <subobjects>
        <object name="line item">
          <fields>
            <field name="product" type="product" key="reference"/>
            <field name="quantity" type="integer"/>
            <field name="total" type="money" required="true"/>
          </fields>
          <config>
            <sql:table>
              <sql:parent-foreign-key name="FK_InvoiceLineItem" update="cascade"/>
            </sql:table>
          </config>
          <doc>
            <summary>Line item child object of the invoice.</summary>
          </doc>
        </object>
      </subobjects>
    </object>
  </objects>
</module>
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Copyright © 2009-2022 Xomega.Net. All rights reserved.