Head Spin Moment ORM many-to-many Help?
I am trying to understand if this is correct way of looking at ORM and a many-to-many relationship.
Below I have two tables. Users and Monsters. A user can have many monsters, and a monster can have many users. Therefore I have a many-to-many relationship. Now I decide to include a join table called mostercollected to normalise the table somewhat. So below is what I come up with.
2* user
3*/
4component output="false" persistent="true" {
5
6property name="user_id" column="user_id" type="numeric"
7ormtype="int" fieldtype="id" generator="increment";
8
9property name="user_name" column="user_name"
10type="string" ormtype="string";
11
12 /* link tables */
13property name="monsters" fieldtype="many-to-many"
14CFC="monsters" FKColumn="user_id" singularname="monsters"
15inversejoincolumn="monster_id" linktable="monstersCollected";
16
17 users function init() output=false{
18 return this;
19 }
20
21}
22
23
24
25
26/**
27* monsters
28*/
29component output="false" persistent="true" {
30
31property name="monster_id" column="monster_id" type="numeric"
32ormtype="int" fieldtype="id" generator="increment";
33
34property name="monster_name" column="monster_name" type="string"
35ormtype="string" ;
36
37/* link tables */
38property name="users" fieldtype="many-to-many" CFC="users"
39FKColumn="monster_id" singularname="monsters"
40 inversejoincolumn="user_id" linktable="monstersCollected";
41
42 monsters function init() output=false{
43 return this;
44 }
45
46}
47
48
49 /* Join table */
50
51
52
53component entityname="UserMonster" persistent="true" accessors="true"
54table="monstersCollected"
55{
56 property name="user_id"
57 fieldtype="id,many-to-one"
58 cfc="users"
59 cascade="all"
60 fkcolumn="user_id";
61
62 property name="monster_id"
63 fieldtype="id,many-to-one"
64 cfc="monsters"
65 cascade="all"
66 fkcolumn="monster_id";
67
68}
Now if I dump a user to screen, yes I can see all the monsters that each user has (cool), but the array continues down more, showing all monsters and all users that belong to that monster, effectively repeating data (bad, right?).
- User1
- Monster2
- User1
- User2
- Monster1
- User1
- User2
- User2
- Monster2
- User1
- User2
- Monster1
- User1
- User2
My concern is that of performance. If every time I want to get a user and I then get monsters + monster again and users that belong to that monster (head spin, moment), this seems like a lot of overhead when all I want is a list of the following...
- User1
- Monster2
- Monster1
- User2
- Monster2
- Monster1
Is this the issue I am making it out to be? Or have fundamentally missed the boat on this one and misunderstood completely?
Sep20



21/09/10 13:00
The term is "lazy loading" so I have been told. Now makes sense!