Modeling HABTM

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

Modeling HABTM

sergio_101
I am currently putting together a Seaside website for my radio show. I am trying to figure out how to model the "has and belongs to many" relationships. 

Each episode can have many tracks, each of these tracks can belong to several episodes.
I want to be able to present a listing of which episodes each track appears in, and a listing of tracks for each episode.

The approach I have seen on this is to create an intermediary object and store a set of ids on this, but this seems a little clunky, and it seems like there would be a clean way to do this.
Am I missing something?
Thanks!


----
peace,
sergio
photographer, journalist, visionary

Public Key: http://bit.ly/29z9fG0
#BitMessage BM-NBaswViL21xqgg9STRJjaJaUoyiNe2dV
http://www.codeandmusic.com
http://www.twitter.com/sergio_101
http://www.facebook.com/sergio101


signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Modeling HABTM

Ben Coman


On Mon, 10 Jun 2019 at 20:21, sergio ruiz <[hidden email]> wrote:
I am currently putting together a Seaside website for my radio show. I am trying to figure out how to model the "has and belongs to many" relationships. 

Each episode can have many tracks, each of these tracks can belong to several episodes.
I want to be able to present a listing of which episodes each track appears in, and a listing of tracks for each episode.

The approach I have seen on this is to create an intermediary object and store a set of ids on this, but this seems a little clunky, and it seems like there would be a clean way to do this.

This is how you'd model many-to-many relationships in relational databases. 
If that is where your data is stored, you may be forced to this.

If you are keeping your design purely in the object-oriented domain, 
although I've not enough experience with it to know if its the best way.

Or you might try relation slots.   Check out SlotExampleMovieAndPersonTest.
which I'd guess is still useful even if I read somewhere it was a bit out of date.

cheers -ben  
Reply | Threaded
Open this post in threaded view
|

Re: Modeling HABTM

sergio_101
Thanks! 

I’ll look at this. I’d like to keep it in the OO domain.. There is no existing data. Everything would be created for this project.


On Jun 10, 2019, at 12:53 PM, Ben Coman <[hidden email]> wrote:

If you are keeping your design purely in the object-oriented domain, 
although I've not enough experience with it to know if its the best way.


----
peace,
sergio
photographer, journalist, visionary

Public Key: http://bit.ly/29z9fG0
#BitMessage BM-NBaswViL21xqgg9STRJjaJaUoyiNe2dV
http://www.codeandmusic.com
http://www.twitter.com/sergio_101
http://www.facebook.com/sergio101


signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Modeling HABTM

Esteban A. Maringolo
In reply to this post by sergio_101
The M:N relationship is pretty straightforward in pure OO design.

You have anEpisode that references a collection with instances of Track.
And each Track has a collection of episodes.

Of course this could be elaborated even further, and have an object
like TrackPlay sitting in between of anEpisode and aTrack.
This TrackPlay object could have extra information, like the timespan
at which it was played, the date (although this could be delegated to
the Episode itself).

It doesn't happen much these days, but a billboard hit was played more
than once, so with a TrackPlay you could log both plays and know when
each one was played.

So
Episode "has many" TrackPlay
TrackPlay "references" Episode and Track

The Track could "has many" Episodes (and ask them for the TrackPlays
of itself) or "has many" references TrackPlay.

And you can make things even more elaborated, but I'd go for the
"TrackPlay" (for the lack of a better name) path.

Regards,

Esteban A. Maringolo

On Mon, Jun 10, 2019 at 9:21 AM sergio ruiz <[hidden email]> wrote:

>
> I am currently putting together a Seaside website for my radio show. I am trying to figure out how to model the "has and belongs to many" relationships.
>
> Each episode can have many tracks, each of these tracks can belong to several episodes.
> I want to be able to present a listing of which episodes each track appears in, and a listing of tracks for each episode.
>
> The approach I have seen on this is to create an intermediary object and store a set of ids on this, but this seems a little clunky, and it seems like there would be a clean way to do this.
> Am I missing something?
> Thanks!
>
>
> ----
> peace,
> sergio
> photographer, journalist, visionary
>
> Public Key: http://bit.ly/29z9fG0
> #BitMessage BM-NBaswViL21xqgg9STRJjaJaUoyiNe2dV
> http://www.codeandmusic.com
> http://www.twitter.com/sergio_101
> http://www.facebook.com/sergio101
>

Reply | Threaded
Open this post in threaded view
|

Re: Modeling HABTM

jgfoster
In reply to this post by sergio_101
Sergio,

When you have a two-way relationship, one option is to keep duplicate information and use it to go from one object to the others. The advantage is rapid lookup, but the disadvantage is the need to update multiple places to maintain consistency (as well as the space it takes). 

Another option is to keep the relationship in only one place and then do a search for the secondary relationship on each lookup. So, each episode has a collection of tracks and the episode to tracks lookup is direct. If you have a track and want to find the episodes, then you query each episode to see if it includes that track. The advantage is simplicity of design and maintenance (the internal structures are never inconsistent; wrong, maybe, but never inconsistent!). 

The disadvantage is extra time doing the lookup. But before you discount this approach, ask yourself what performance penalty this will cost. How many episodes? How many tracks? Do you have less than 10K of each? How many milliseconds does it take to do a “brute-force” search? I strongly recommend:
* Make it work, [and only then]
* Make it right, [and only then, if really needed]
* Make it fast
Do the simplest thing that can possibly work, and that is to have one one set of references. Don’t add complexity till you need it. Hide (encapsulate) the implementation so you can change it later if needed. 

Another approach is to use a tool that provides automatic references. GemStone includes indexes that are managed by the system, so you can just ask the index for all episodes that reference a track. You only update one data structure but the system manages more.

James

On Jun 10, 2019, at 5:20 AM, sergio ruiz <[hidden email]> wrote:

I am currently putting together a Seaside website for my radio show. I am trying to figure out how to model the "has and belongs to many" relationships. 

Each episode can have many tracks, each of these tracks can belong to several episodes.
I want to be able to present a listing of which episodes each track appears in, and a listing of tracks for each episode.

The approach I have seen on this is to create an intermediary object and store a set of ids on this, but this seems a little clunky, and it seems like there would be a clean way to do this.
Am I missing something?
Thanks!


----
peace,
sergio
photographer, journalist, visionary

Public Key: http://bit.ly/29z9fG0
#BitMessage BM-NBaswViL21xqgg9STRJjaJaUoyiNe2dV
http://www.codeandmusic.com
http://www.twitter.com/sergio_101
http://www.facebook.com/sergio101


Reply | Threaded
Open this post in threaded view
|

Re: Modeling HABTM

sergio_101
In reply to this post by Esteban A. Maringolo
when you say references, how does this look in pharo? I don’t know that I have used such a relationship.

I was thinking that an object would have an instance variable that was an ordered collection of objects.. So having one object belong to two parent objects could be tricky.



On Jun 10, 2019, at 1:27 PM, Esteban Maringolo <[hidden email]> wrote:

You have anEpisode that references a collection with instances of Track.
And each Track has a collection of episodes.

----
peace,
sergio
photographer, journalist, visionary

Public Key: http://bit.ly/29z9fG0
#BitMessage BM-NBaswViL21xqgg9STRJjaJaUoyiNe2dV
http://www.codeandmusic.com
http://www.twitter.com/sergio_101
http://www.facebook.com/sergio101


signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Modeling HABTM

Esteban A. Maringolo
By reference I mean an instance variable. So it's the same thing.

But it's not that one object has two "parents" (this isn't a tree like
structure).
The object (it is, the Track) has two references to the Episodes where
it appears, such reference happens because aTrack as a instance
variable "holding" (aka "referencing") aCollection, and this
collection "contains" (it is, references) two elements, that are the
Episodes in which a track appears.

For the sake of simplicity, you should define who must perform the
"maintenance" of the collections on only one sides. In my opinion it
should be the Episode.

E.g.
Episode>>addTrackToPlaylist: aTrack
  self playlist add: aTrack.
  aTrack includeInEpisode: self

Episode>>playlist
  playlist ifNil: [playlist := OrderedCollection new].


Track>>includeInEpisode: anEpisode
   self episodes add: anEpisode

Track>>#episodes
   episodes ifNil: [episodes := Set new]


You can see that #includeInEpisode: doesn't add itself to the Episode,
because it is very likely that the interaction is started onto the
Episode and not the Track. Otherwise to avoid a recursion you should
have a condition like:

Episode>>addTrackToPlaylist: aTrack
  (self playlist includes: aTrack) ifFalse: [
    self playlist add: aTrack.
    aTrack includeInEpisode: self ]

Track>>includeInEpisode: anEpisode
   self episodes add: anEpisode.
   anEpisode addTrackToPlaylist: self




The paper about "Mutual Friends" talks about the pro's and cons of
different approaches.

Esteban A. Maringolo

On Mon, Jun 10, 2019 at 2:40 PM sergio ruiz <[hidden email]> wrote:

>
> when you say references, how does this look in pharo? I don’t know that I have used such a relationship.
>
> I was thinking that an object would have an instance variable that was an ordered collection of objects.. So having one object belong to two parent objects could be tricky.
>
>
>
> On Jun 10, 2019, at 1:27 PM, Esteban Maringolo <[hidden email]> wrote:
>
> You have anEpisode that references a collection with instances of Track.
> And each Track has a collection of episodes.
>
>
> ----
> peace,
> sergio
> photographer, journalist, visionary
>
> Public Key: http://bit.ly/29z9fG0
> #BitMessage BM-NBaswViL21xqgg9STRJjaJaUoyiNe2dV
> http://www.codeandmusic.com
> http://www.twitter.com/sergio_101
> http://www.facebook.com/sergio101
>

Reply | Threaded
Open this post in threaded view
|

Re: Modeling HABTM

Pharo Smalltalk Users mailing list
In reply to this post by sergio_101
I've done a couple cheap and cheerful ORMs in various languages.   I always punt on doing the relationships because finishing the app is more urgent than writing a whole framework.  I just add an instance method that executes the appropriate SQL to fetch the related objects.  

You can spend a lot of time trying to do this in a generic declarative way, or you can just write a query as an instance method and get on with finishing your app.

On Jun 10, 2019, at 5:20 AM, sergio ruiz <[hidden email]> wrote:

I am currently putting together a Seaside website for my radio show. I am trying to figure out how to model the "has and belongs to many" relationships. 

Each episode can have many tracks, each of these tracks can belong to several episodes.
I want to be able to present a listing of which episodes each track appears in, and a listing of tracks for each episode.

The approach I have seen on this is to create an intermediary object and store a set of ids on this, but this seems a little clunky, and it seems like there would be a clean way to do this.
Am I missing something?
Thanks!


----
peace,
sergio
photographer, journalist, visionary

Public Key: http://bit.ly/29z9fG0
#BitMessage BM-NBaswViL21xqgg9STRJjaJaUoyiNe2dV
http://www.codeandmusic.com
http://www.twitter.com/sergio_101
http://www.facebook.com/sergio101


Reply | Threaded
Open this post in threaded view
|

Re: Modeling HABTM

sergio_101
In reply to this post by Esteban A. Maringolo
ah! 

this exactly how i ended up putting it together…

and thanks, Jim for the pointers…

I just wanted to make sure I wasn’t totally missing something..



On Jun 10, 2019, at 1:53 PM, Esteban Maringolo <[hidden email]> wrote:

But it's not that one object has two "parents" (this isn't a tree like
structure).
The object (it is, the Track) has two references to the Episodes where
it appears, such reference happens because aTrack as a instance

----
peace,
sergio
photographer, journalist, visionary

Public Key: http://bit.ly/29z9fG0
#BitMessage BM-NBaswViL21xqgg9STRJjaJaUoyiNe2dV
http://www.codeandmusic.com
http://www.twitter.com/sergio_101
http://www.facebook.com/sergio101


signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Modeling HABTM

sergio_101
In reply to this post by jgfoster
these numbers are super tiny.. 

25 episodes so far with about 15 tracks per episode..



On Jun 10, 2019, at 1:36 PM, James Foster <[hidden email]> wrote:

The disadvantage is extra time doing the lookup. But before you discount this approach, ask yourself what performance penalty this will cost. How many episodes? How many tracks? Do you have less than 10K of each? How many milliseconds does it take to do a “brute-force” search? I strongly recommend:

----
peace,
sergio
photographer, journalist, visionary

Public Key: http://bit.ly/29z9fG0
#BitMessage BM-NBaswViL21xqgg9STRJjaJaUoyiNe2dV
http://www.codeandmusic.com
http://www.twitter.com/sergio_101
http://www.facebook.com/sergio101


signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Modeling HABTM

Christopher Fuhrman-2
Using an intermediate object "Play" (abstraction of a track being played during an episode), a UML diagram might look like:

image.png


On Mon, 10 Jun 2019 at 23:02, sergio ruiz <[hidden email]> wrote:
these numbers are super tiny.. 

25 episodes so far with about 15 tracks per episode..



On Jun 10, 2019, at 1:36 PM, James Foster <[hidden email]> wrote:

The disadvantage is extra time doing the lookup. But before you discount this approach, ask yourself what performance penalty this will cost. How many episodes? How many tracks? Do you have less than 10K of each? How many milliseconds does it take to do a “brute-force” search? I strongly recommend:

----
peace,
sergio
photographer, journalist, visionary

Public Key: http://bit.ly/29z9fG0
#BitMessage BM-NBaswViL21xqgg9STRJjaJaUoyiNe2dV
http://www.codeandmusic.com
http://www.twitter.com/sergio_101
http://www.facebook.com/sergio101

Reply | Threaded
Open this post in threaded view
|

Re: Modeling HABTM

Jeff Gray
This may not be a very object thing to say but if you have a relational
database with tables for episode, track and play like in Christopher's
diagram, you may not need a class to model play. As it is just a repository
for a set of links I might just have a method on an Episode class that calls
the sql to return a collection of Tracks and vice versa.



--
Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html

Reply | Threaded
Open this post in threaded view
|

Re: Modeling HABTM

sergio_101
Oh! i am trying to do everything in pharo/seaside.. 

i am not using a database at all..

i am working on getting this set up in gemstones, but i am still really confused in that area..



On Jun 12, 2019, at 10:35 PM, Jeff Gray <[hidden email]> wrote:

diagram, you may not need a class to model play. As it is just a repository
for a set of links I might just have a method on an Episode class that calls
the sql to return a collection of Tracks and vice versa. 

----
peace,
sergio
photographer, journalist, visionary

Public Key: http://bit.ly/29z9fG0
#BitMessage BM-NBaswViL21xqgg9STRJjaJaUoyiNe2dV
http://www.codeandmusic.com
http://www.twitter.com/sergio_101
http://www.facebook.com/sergio101


signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Modeling HABTM

Christopher Fuhrman-3
In reply to this post by Jeff Gray


On Thu, Jun 13, 2019, 04:35 Jeff Gray <[hidden email]> wrote:
This may not be a very object thing to say but if you have a relational
database with tables for episode, track and play like in Christopher's
diagram, you may not need a class to model play. As it is just a repository
for a set of links

I suppose it depends on the requirements for the software. If you wanted to know the start time (relative to the start of the episode) of the Play, that would be an attribute for the class. 

I might just have a method on an Episode class that calls
the sql to return a collection of Tracks and vice versa.



--
Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html