[CDBI] Re: Retrieve only one result from a has_many

Peter Speltz peterspeltz at gmail.com
Sun Dec 18 19:56:45 GMT 2005


On 12/17/05, Matt Adams <matt.adams at morcor.com> wrote:


> MyApp::DB::Deadline->has_a( versioncode => 'MyApp::DB::Version' );
> MyApp::DB::Version->has_many( deadlines => 'MyApp::DB::Deadline' );

> In other words, is it possible to limit the results returned from
> $object->deadlines based on information I supply when I create the CDBI
> relationships?
>

Patches for this have been available for a couple years now. It is in
the CDBI 3.10 at least but not documented. I think it is noted in the
changes file.   Anyway, get the latest CDBI::Relationship::HasMany and
 try :

MyApp::DB::Deadline->has_a( versioncode => 'MyApp::DB::Version' );
 MyApp::DB::Version->has_many( deadlines => 'MyApp::DB::Deadline' ,  
{constraint => { col1 => XXX , col2 => xxx} );

That should do what you want. I use this some when I have 2 distinct
related groupings.  Similarly, I wrote a might_have  module that I
have yet to put on CPAN that allows contraints.  It is MihtHaveWC.  I
use it to specify the FK and have Multi Keys in the might have.  
Here's how i use it :

__PACKAGE__->might_have_wc( 'billing_address' => 'DB::Address' =>
    qw/street city state zip county addr_type/ ,
    { _FK_ => 'owner_id', owner_table => 'customer', category => "Billing",
      is_active => 1}
);

---
pjs




More information about the ClassDBI mailing list