[CDBI] How can you query the has_many relationships from a CDBI object?

[prev] [thread] [next] [Date index for 2005/11/17]

From: Phillip Moore
Subject: [CDBI] How can you query the has_many relationships from a CDBI object?
Date: 19:44 on 17 Nov 2005
Greetings, list members...

This should be a simple question.

I have defined one or more relationships using has_many(), and these  
are defined in the class definition itself, of course.  In an  
application, I'd like to be able to query those relationships, and  
take appropriate action.

For example, I set cascade => 'Fail', because in the system I am  
writing, you need to manually take action to remove the "downstream"  
dependent objects, before removing the upstream object.   I am  
writing a generic method to check an object for downstream  
dependents, and provide for a more user-friendly warning than the  
errors CDBI will spit out if you attempt to perform a cascading  
delete accidentally.

For example, suppose I have:

Foo->has_many( bars => 'Bar' , { cascade => 'Fail' } );
Foo->has_many( bazs => 'Baz', { cascade => 'Fail' } );

and then later in my code somewhere, I want to query that  
information, namely that Foo has a pair of has_many() relationships.   
This will let my generic delete method be truly generic, if I can  
query this information.

while ( my ($method,$foreign_class) = each %has_many ) {
     my @foreign_objs = $object->$method();
     if ( @foreign_objs ) {
         # User-friendly feedback goes here...
     }
}

So the question is: how can I query the %has_many relationships?   So  
far, it looks like I need to keep track of these as class data, and  
provide a class method to access them, but it just seems to me that  
this information must be queryable somehow.




_______________________________________________
ClassDBI mailing list
ClassDBI@xxxxx.xxxxxxxxxxxxxxxx.xxx
http://lists.digitalcraftsmen.net/mailman/listinfo/classdbi

[CDBI] How can you query the has_many relationships from a CDBI object?
Phillip Moore 19:44 on 17 Nov 2005

Generated at 08:22 on 28 Nov 2005 by mariachi v0.52