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

William Ross will at spanner.org
Tue Nov 22 22:22:59 GMT 2005


On 17 Nov 2005, at 19:44, Phillip Moore wrote:

> Greetings, list members...
>
> This should be a simple question.
>
> <snip>
> 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.

It is.

   $class->meta_info(has_many => $relname);

will return something like:

$VAR1 = bless( {
     'foreign_class' => 'Delivery::Group::Document',
     'name' => 'has_many',
     'args' => {
         'mapping' => [
             'usergroup'
         ],
         'foreign_key' => 'document',
         'order_by' => undef
     },
     'class' => 'Delivery::Document',
     'accessor' => 'usergroups'
}, 'Class::DBI::Relationship::HasMany'


and shortening it to:

   $class->meta_info('has_many');

will return a reference to a hash of several such HasMany objects  
keyed by accessor name.

Around the cdbi modules you'll see variations on this code:

   my $meta = $class->meta_info($reltype => $accessor);
   my ($f_class, $f_key, $args) = ($meta->foreign_class, $meta->args-> 
{foreign_key}, $meta->args);

which should give you all the ingredients you need.

best,

will






More information about the ClassDBI mailing list