[CDBI] Re: Searching using a might_have relationship.

Matt S Trout dbix-class at trout.me.uk
Sat Oct 21 00:49:51 BST 2006

Edward J. Sabol wrote:
> Oliver Jeeves wrote::
>> Basically, I want to use a value from a might_have relationship to
>> search, and I don't want to have to do multiple SQL queries to do it.
>> Is there an easy way to do this?
> No, I don't believe so. Support for might_have relationships could be better
> in CDBI. I think it's a weakness anyway. They are harder to populate than a
> has_many() and you cannot easily search() for them via the parent class.
> You have a couple of options, I think.
> (1) Instead of
>  MyDB::A->search(other => 'foo');
>     you can use B's search() method and then MyDB::A->retrieve() the IDs
>     that you found in B, like so:
>  my @others = map { MyDB::A->retrieve($_->id) } MyDB::B->search(other => 'foo');
>     Relatively easy, but not very efficient.
> (2) You could use set_sql(), like so:
>  MyDB::A->set_sql('others' => <<"");
> FROM   __TABLE(MyDB::A=a)__,
>        __TABLE(MyDB::B=b)__,
> WHERE  __JOIN(a b)__
> AND    b.other = ?
>  MyDB::A->search_others('foo');
>     Refer to http://wiki.class-dbi.com/wiki/Using_joins for more info.
> (3) You could use Class::DBI::Plugin::DeepAbstractSearch. Not sure if it
>     supports might_have's or not, though I suspect it does.
> http://search.cpan.org/~sriha/Class-DBI-Plugin-DeepAbstractSearch/DeepAbstractSearch.pm

Class::DBI::Sweet certainly does and is IMO a rather cleaner implementation, 
albeit with a silly name.

      Matt S Trout       Offering custom development, consultancy and support
   Technical Director    contracts for Catalyst, DBIx::Class and BAST. Contact
Shadowcat Systems Ltd.  mst (at) shadowcatsystems.co.uk for more information

+ Help us build a better perl ORM: http://dbix-class.shadowcatsystems.co.uk/ +

More information about the ClassDBI mailing list