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

Edward J. Sabol sabol at alderaan.gsfc.nasa.gov
Fri Dec 16 22:59:07 GMT 2005

> I have the following CDBI relationships set up like this:
> MyApp::DB::Deadline->has_a( versioncode => 'MyApp::DB::Version' );
> MyApp::DB::Version->has_many( deadlines => 'MyApp::DB::Deadline' );
> Now, when I retrieve a particular version via
> $object = $MyApp::DB::Version->retrieve( 23 );
> I get back a bunch of nifty looking objects with blessed references to
> the related deadlines (which is cool if I need to iterate over a number
> of deadlines and whatnot).

You mean 

   $object = MyApp::DB::Version->retrieve( 23 );

of course, and I think you left out a line there, because retrieve will not
give you "a bunch of nifty looking objects." The retrieve() method should
only give you a single object. Presumably, you meant something like:

   @deadlines = $object->deadlines();

> Here is my problem:
> I have a situation where I must retrieve one particular deadline for a
> specific version and even though I have a has_many relationship here I
> only want to get the value of the specific deadline according to a
> column value in MyApp::DB::Deadline (say the column is something like
> deadline.dset).
> So, when I retrieve a version like the $class->retrieve() above I don't
> want a blessed reference to ALL of the related deadlines; I only want
> to get back a blessed reference (or perhaps not a reference at all but
> a value) of the related deadlines with the deadline.dset value of 5.
> Is there a simple way to go about this?

Did you see this section in the documentation?


Assuming dset in the deadline table is unique for a given versioncode, maybe
something like this:

   ($specific_deadline) = $object->deadlines(dset => 5);

Hope this helps,

More information about the ClassDBI mailing list