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

Phillip Moore phil at yume.org
Thu Nov 17 19:44:55 GMT 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.







More information about the ClassDBI mailing list