iphone - core data model
I am new to core data. I am banging my head on the wall trying to understand how should I design the data model for this application.
I have to create a database for books.
Books have the following characteristics:
.englishName
.italianName
.frenchName
.spanishName
.number
Books can be of two kind: free or non-free. Books can be sold individually or in bundles. A bundle can represent a bunch of books. A book can be in one or more bundles.
What I need:
- The ability to mark as paid a bundle or/and book.
- the ability to retrieve all non-free or all free books, all books from a bundle, all books that were bought.
How I imagine this could be done: I would create 3 entities: Bundle, Product and Type and make all products bundles, I mean, a bundle with 1 product or with multiple products.
Bundle
.Number
.Paid
-relation to product (fromBundle) to-many
Product
.nameEN
.nameIT
.nameFR
.nameSP
.Number
-fromBundle (relation to Bundle) to-many
-fromType (relation to Type)
Type
.kind
-relation to Product... to-many
But I am n开发者_JS百科ot sure this is not the way, simply because there's no way to define a bundle name in all 4 languages and if I add those attributes to the bundle, I will end with a lot of redundancy...
please help meeeeeee! thanks.
You may be overcomplicating this by introducing Product and Type - all you should really need are the Book and Bundle entities, with a many-to-many relationship between them. Then, to deal with the use cases you presented:
- Marking a bundle or book paid: for a book, simply mark the book paid; for a bundle, mark all its books paid.
- For data retrieval:
- To retrieve all non-free or all free books, just get instances of the Book entity with the
free
attribute set to the appropriate value (or theprice
value zero or nonzero, depending on how you're storing this attribute) - To retrieve all books from a given bundle, just call
[bundle books]
- the many-to-many relationship gives you this directly - To retrieve all books that were bought, just look for books marked
paid
- To retrieve all non-free or all free books, just get instances of the Book entity with the
This way, you can keep your four name attributes on the Book itself and not worry about them (unless you have to name Bundles, but those should be distinct names anyway). Furthermore, it's conceptually simple - you don't have to worry about what precisely constitutes a Product or semantics of that manner.
精彩评论