Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Deleting the child records of a form
Message
De
23/05/2003 03:24:17
Cetin Basoz
Engineerica Inc.
Izmir, Turquie
 
 
À
22/05/2003 14:34:11
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
00791822
Message ID:
00792079
Vues:
27
>Hello cetin, Please help me on this.
>I have this code in the class.
>
>This code is used to delete the master record and after deleting the master record .it will look for all the child tables of this form and has to delete those child records also.
>
>In the form's dataenvironment i have the event used (beforeopenTables Event) in that all the cursor names are mentioned.
>
>But when i do the delete on the form it reads this code when it comes to the below point.
>
>=AMEMBERS(A_Cursors,THISFORMset.dataenvironment,1)
> =ASORT(A_Cursors, 2)
> nStartpos=ASUBSCRIPT(A_Cursors, ASCAN(A_Cursors, "Object"),1)
>
>--COMMENT---Some thing is not working here do i need to mention about the beforeopentables event in the above code please tell me can i do this(THISFORMset.dataenvironment.beforeopentables)
>
>And in the A_Cursors i am getting "INIT" i guess it is looking for init event of dataenvironment instead it should look in beforeopentables event of the form's dataenvironment.
>
>
>
>**********I am pasting this code which is where it is not finding any cursors at all and bypassing the if condition****************
>
>
>IF DeleteRecord
> if deleted() THEN
> RECALL
> ELSE
> DELETE
> ENDIF
>
> mParentId = EVAL(this.MasterTableId)
>set step on
>* Make a list of all the cursors in the Data Environment
> =AMEMBERS(A_Cursors,THISFORMset.dataenvironment,1)
> =ASORT(A_Cursors, 2)
> nStartpos=ASUBSCRIPT(A_Cursors, ASCAN(A_Cursors, "Object"),1)
>set step on
>* find the child records and delete or recall
> FOR I = nStartpos TO ALEN(A_Cursors,1)
> IF A_Cursors(I,2) = "Object"
> cObjClass = "THISFORMset.DATAENVIRONMENT."+A_Cursors(I,1)+".class"
> IF EVAL(cObjClass)="Relation"
> cObjName="THISFORMset.DATAENVIRONMENT."+ A_Cursors(I,1)+".parentAlias"
> cObjChild = "THISFORMset.DATAENVIRONMENT."+ A_Cursors(I,1)+".childAlias"
> cObjChildOrder = "THISFORMset.DATAENVIRONMENT."+ A_Cursors(I,1)+".childorder"
> Parent_Name=EVAL(cObjName)
> Child_Name = eval(cObjChild)
> Child_Order = eval(cObjChildOrder)
> IF UPPER(Parent_name) = UPPER(sTable)
>
>***********************************END**********************************************


Raj,
You could collect child aliases with a code like :
Local Array arrChilds[1,3]
lnMembers = Amembers(arrObjects, ThisformSet.DataEnvironment,2)
For ix = 1 To lnMembers
  With Evaluate('thisformset.dataenvironment.'+arrObjects[ix])
    If Lower(.BaseClass) = 'relation' And ;
       Lower(.ParentAlias) = Lower(tcParentAlias)
      Dimension arrChilds[Iif(Type('arrChilds[1,1]')='L',0,1)+;
          alen(arrChilds,1),3]
      arrChilds[Alen(arrChilds,1),1] = .ChildAlias
      arrChilds[Alen(arrChilds,1),2] = .RelationalExpr
      arrChilds[Alen(arrChilds,1),3] = .ChildOrder
    Endif
  Endwith
Endfor
However I wrote it just to show code, not to mean do it.
IMHO it's dangerous to have code like this with delete. Because relations are not meant to be always from parent to child. ie: Customer is parent of orders if you look persistent relations in DBC. But some developers (like me) might make a reverse relation in DE, making the orders parent. IOW DE relations doesn't tell you anything in fact which one is the parent (you might find out though).

If you want to do cascading deletes use referential integrity. Or a stored procedure of your own. Sometimes a child might have multiple parents and you might want to delete its records only if all or some of its foreign keys are orphaned not when just one.
From performance point you might even want to do child deletions only in maintanence routines too (while packing tables). ie:

use customer exclusive
pack
select 0
use orders
*delete from orders where cust_id not in (select cust_id from customer)
delete for !seek(cust_id,'customer','cust_id')
pack
select 0
use orditems
delete for !seek(order_id,'orders','order_id')
pack

PS: Didn't understand the comment.
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform