V4RB examples
Erik Mueller-Harder
lists at praxisworks.com
Fri Aug 8 23:11:26 CDT 2003
Tim Lambert wrote:
> Hi Can anyone help with a beginner's desire to figure out a 1 to
> many set up (for RB 5.2)? I can get Valentina working easily
> with a single table but the 1 to many example just doesn't happen
> for me. Is there a better set of examples I could learn from
> anywhere ? I'm sure is a simple thing to get the many table to
> track the linked 'ID' field but....how?
Sure!
The magic is in the "object pointer" field in your many table; set it up correctly, and you hardly have to think about the relationship again. The Valentina Kernel documentation explains the setup pretty simply on page 39; also see pages 56-58 of the V4RB reference.
In short, though, just set up an object pointer field in a constructor function of your many table like this:
fieldName = new VObjectPtr("fieldName",
database.baseObject("oneTable"), kV_Restrict)
(all on one Rb line, of course, where "fieldName" is whatever you want to call your pointer field and "oneTable" is the name of the one-table you're relating back to). The field on "oneTable" that you're relating to is the "recID" field, whose value you have no control over -- it's automatic.
kV_Restrict, kV_Cascade, and kV_SetNull (choose one!) define what will occur to the records on the many table when the single record they're related to on the one table gets deleted:
kV_Restrict means you canNOT delete the one record if related many records exist.
kV_Cascade means that all the related records on the many table get deleted when you delete the one record.
kV_SetNull means that the many records get their objectPtrs set to 0 if the related one record gets deleted -- they're orphaned, in other words.
If you have a "one" table called "personBO" and a "many" table called "taskBO", it might look like this:
PersonBO TaskBO
-------- ------
recID <--------- personPtr
firstName taskDate
lastName taskTime
taskDescription
After creating a record on PersonBO, create as many records on TaskBO as you need, populating personPtr with recID from personBO (watch line breaks!):
personCursor = db.SQLSelect("select recID, * from personBO
where recID = 0, kV_Server, kV_ReadWrite, kV_Random)
personCursor.varCharField("firstName").value = "Erik"
personCursor.varCharField("lastName").value = "Mueller-Harder"
if not personCursor.Update then
// error handling here
end if
taskCursor = db.SQLSelect("select * from taskBO where
recID = 0, kV_Server, kV_ReadWrite, kV_Random)
taskCursor.objectPtr("personPtr").value =
personCursor.ULongfield("recID").value
[populate other task fields here]
if not taskCursor.Update then
// error handling here
end if
>From that point on, the data are related -- the relationship need not explicitly be referred to. So:
vc = db.SQLSelect("select firstName, lastName, taskDate from
personBO, taskBO where lastName = 'Mueller-Harder'")
will give you a cursor containing all persons in personBO with the last name of Mueller-Harder, and all of their related tasks -- you can leave out the customary where clause " and taskBO.personPtr = personBO.recID" altogether.
I hope this is enough to get you started.
Erik
More information about the Valentina
mailing list