Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- FAST TEXT SEARCH for Clipper v.2.0 - <b>one to many relations</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
   One to Many Relations

   The description of handling relations (Searching Multiple Fields 
   Across Related DBFs) is the basis for handling all relations, 
   regardless of whether the relation is a One to Many, a Many to One, or 
   a One to One. Using the previous example as a foundation the following 
   code fragments will help to illustrate how to program a One To Many 
   using CFTS.

   Example 1 - Building One To Many Key

   xhandle = CftsCrea( office.ia, 10, 3, .t., 1 )
   select 0
   use name index name      && the 'many' file
   select 0
   use office               && the primary or 'one' file
   set relation to office-name into name   && assume that 'name' is
                            && a common key between the two files
   do while !eof()          && test the primary file
       instring = GetFields()
       rec = CftsAdd( xhandle, instring )  && record # is from
       primary file
       skip
   enddo
   close all
   CftsClose( xhandle )
   quit
    
   function GetFields
   local xstring, tkey
   xstring = office-name              && get field(s) from primary
   file
   select name                        && select 'many' file
   do while office-name = name-name   && read all 'many' keys
       xstring = xstring + name-name  && concatenate values
       skip
   enddo
   select office                      && restore primary select
   return( xstring )

   Example 2 - Searching a One To Many Relation

   srchstring = John A. Reesman
   xhandle = CftsOpen( office.ia, 10, .t. )
   select 0
   use name index name  && the 'many' file
   select 0
   use office                 && the primary or 'one' file
   set relation to office-name into name
   CftsSet( xhandle, srchstring )
   xrec = CftsNext( xhandle ) && returns record #s from
   primary file
   do while xrec > 0          && got a hit
   goto xrec                  && position to primary record
   xstring = GetFields()      && get data from primary file and
                              && any related records
   if CftsVeri( xhandle, xstring, srchstring, 3 ) = 1
   ? found one!!
   wait
   endif
   enddo
   close all
   CftsClose( xhandle )

   The point to keep in mind when handling these cases is that CFTS can 
   store only one record number for each logical record indexed. 
   Consequently, you want to build the index so that the record order in 
   which you index the data is the one you want to use to access the 
   records.


See Also: Searching Multiple Fields Across Related DBFs

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson