Re: Fetching the next child object [NEWBIE]
Re: Fetching the next child object [NEWBIE]
- Subject: Re: Fetching the next child object [NEWBIE]
- From: Chuck Hill <email@hidden>
- Date: Thu, 26 Jun 2003 23:43:47 -0700
Hi,
As a general rule with WO/EO:
If you starting thinking about database keys are you wandering off the
straight and true path.
Try to think in terms of objects and their relationships. Force your
objects to have responsibilities. In your example I would have modelled
Musician to know their albums (have a relationship albums(). Album would
know the Musician, cleverly called musician() and the songs on the album as
songs(). A song of course should know it's album(). Does this sound like
your EOModel? If not the rest might show you why it should.
Now that you know the musician, aMusician, you can easily fetch exactly the
album that you want with this:
NSArray parameters = new NSMutableArray(theTitle);
parameters.addObject(aMusician);
Album theAlbum = (Album)
EOUtilities.objectWithQualifierFormat(ec,
"Album",
"title = %@ and musician = %@",
parameters);
But wait! The Musician already knows their albums, why not just ask them
for a particular one?
(on Musician.java):
public Album albumNamed(String aName) {
EOQualifier titleQualifier =
new EOKeyValueQualifier("title,
EOQualifier.QualifierOperatorEqual,
aName);
NSArray selectedAlbums =
EOQualifier.filteredArrayWithQualifier(albums(),
titleQualifier);
// Assumes only zero or one matches!
return selectedAlbums.count() > 0 ? selectedAlbums.objectAtIndex(0) :
null;
}
A linear search will be more efficient as it can stop when it finds a
match. filteredArrayWithQualifier will look at each element. I used the
above code as it shows off some useful methods.
If your situation is different and there are many albums and there are
optimization indexes on the database I'd expect the fetch will give better
performance (but you would want to test that assumption, I could be wrong).
HTH
Chuck
At 01:52 AM 27/06/2003 -0400, Goodbye Bill wrote:
>Is it possible to fetch the next child object without having to re-specify
>parameters of the parent object?
>
>DETAILS:
>1. I have three objects in my database that have a parent-child
>relationship. For now, let's say they are:
>
> Musician
> Album
> Song
>
>2. I have fetched a specific "Musician" object using an EOFetchSpecification
>and have that object loaded in memory;
>
>3. I would like to simply get a single Album by specifying the "Title."
>
>WHERE I AM:
>The only way I can figure to do this is pull the ID of the Musician object
>and then use an AndQualifier with the "Title" AND the "ID" of the parent
>musician.
>
>I know there must be a better (and simpler) way. Any examples would be
>greatly appreciated. Please remember, I'm coming from a Microsoft/SQL
>environment so it may simply be natural for me to stray back there.
>
>Thanks, in advance. =S
>_______________________________________________
>webobjects-dev mailing list | email@hidden
>Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/webobjects-dev
>Do not post admin requests to the list. They will be ignored.
>
--
Chuck Hill email@hidden
Global Village Consulting Inc. http://www.global-village.net
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.