Re: Fetch Only Non-Empty To-Many
Re: Fetch Only Non-Empty To-Many
- Subject: Re: Fetch Only Non-Empty To-Many
- From: Art Isbell <email@hidden>
- Date: Wed, 9 Jun 2004 12:17:41 -1000
On Jun 9, 2004, at 10:44 AM, Hunter Hillegas wrote:
I've experimented with this some more and still can't get it to work.
Is what I am trying to do impossible?
On Jun 8, 2004, at 7:09 PM, Hunter Hillegas wrote:
Thanks for the feedback. I gave this a try and didn't get the SQL I
expected... There WHERE clause doesn't even reference the
relationship:
SELECT t0.previous_on_property, t0.rec_num, t0.slot_club_phone,
t0.street_address, t0.toll_free_phone, t0.us_state, t0.website_url,
t0.zip_code FROM hotel t0 WHERE t0.rec_num is not NULL ORDER BY
UPPER(t0.hotel_sort_name) ASC
Did I make a mistake somewhere?
My/your code:
NSMutableArray sortOrderings = new NSMutableArray();
EOSortOrdering hotelSortNameSortOrdering =
EOSortOrdering.sortOrderingWithKey("hotelSortName",
EOSortOrdering.CompareCaseInsensitiveAscending);
sortOrderings.addObject(hotelSortNameSortOrdering);
NSArray bindings = new NSArray(new Object
[]{NSKeyValueCoding.NullValue});
EOQualifier q =
EOQualifier.qualifierWithQualifierFormat("photoGallery != %@",
bindings);
EOFetchSpecification fs = new EOFetchSpecification("Hotel",
q, sortOrderings);
theHotelWithPicturesList = new
NSMutableArray(editingContext.objectsWithFetchSpecification(fs));
On 09/06/2004, at 8:39 AM, Hunter Hillegas wrote:
I have an entity, Hotel, with a to-Many relationship to another
entity, PhotoGallery.
I am trying to construct a fetch in EOModeler that returns all of
the hotels that have at least one photo gallery (i.e., no hotels
without photo galleries).
Is there an easy way to do this?
When I read the proposed solution, I thought that it wouldn't work,
but I wanted someone to confirm this before I embarrassed myself again.
This may be one of the more common SQL queries that developers seem to
need to use but that I don't believe is easy to do with EOF without
fetching more objects than needed and then doing local filtering or by
writing DB-specific code.
I think the SQL that needs to be generated is something like (please
correct me if I'm wrong)
SELECT * FROM Hotel t0 WHERE t0.rec_num in (SELECT * FROM PhotoGallery
t1 WHERE t1.hotel_rec_num is not NULL)
where t0.rec_num is assumed to be the Hotel primary key and
t1.hotel_rec_num, the PhotoGallery's foreign key to Hotel.
This SQL suggests several non-ideal EOF strategies. Fetch all
PhotoGalleries in which the hotel relationship is not null. Ask the
EODatabaseContext to batch fetch the hotel relationship to avoid a
bunch of individual hotel fetches if you plan to ask for hotel details.
Then send the PhotoGallery array a valueForKey("hotel") message,
create an NSSet from the array of hotel objects to filter duplicates,
and get a hotel array from the set. Downsides: possibly unneeded
PhotoGallery objects are fetched and they might be very large objects.
You could use a database-specific solution by creating a fetch spec
with a custom query (the DB-specific SQL that you want sent to the DB
by EOF). See "Using a Custom Query" section of the EODatabaseContext
Javadoc (you didn't expect to look at the EOFetchSpecification Javadoc
for this, did you ?-)
There are other approaches (e.g. raw fetching) that could also be
used, but I don't know of anything particularly elegant. Anyone?
Aloha,
Art
_______________________________________________
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.