]>
Commit | Line | Data |
---|---|---|
6527f429 DM |
1 | /**\r |
2 | * This class and its derivatives describe how two entities are related to each other.\r | |
3 | * Associations have the following forms:\r | |
4 | * \r | |
5 | * * *{@link Ext.data.schema.ManyToOne many-to-one}*\r | |
6 | * * *{@link Ext.data.schema.ManyToMany many-to-many}*\r | |
7 | * * *{@link Ext.data.schema.OneToOne one-to-one}*\r | |
8 | *\r | |
9 | * Associations are first-class objects in a `{@link Ext.data.schema.Schema schema}` but\r | |
10 | * are not created directly. They are created based on {@link Ext.data.field.Field#reference}\r | |
11 | * properties but also on {@link Ext.data.schema.ManyToMany} declarations.\r | |
12 | * \r | |
13 | * Associations have unique names within the `schema` as do {@link Ext.data.Model entities}.\r | |
14 | * In many cases, the association names can be generated. These names have uses beyond the\r | |
15 | * basic needs of tracking such as when communicating with the server. If the generated\r | |
16 | * names are not satisfactory, they can be given explicitly or the default naming can be\r | |
17 | * replaced by implementing a custom `Ext.data.schema.Schema` class.\r | |
18 | *\r | |
19 | * # Life Cycle\r | |
20 | *\r | |
21 | * Intimately connected with many associations is the concept of life-cycle. It is often\r | |
22 | * the case that one entity is "owned" by another so that if the owner is to be deleted,\r | |
23 | * the owned entity must also be deleted.\r | |
24 | * \r | |
25 | * There are also associations that work in the reverse direction. That is, the presence of\r | |
26 | * an associated entity prevents the other entity from being deleted.\r | |
27 | * \r | |
28 | * Finally, some associations just need to be dissolved if one of the related entities is\r | |
29 | * deleted. This is the case in a {@link Ext.data.schema.ManyToMany many-to-many}\r | |
30 | * association, but can also be for others if the `reference` field can be set to `null`.\r | |
31 | * \r | |
32 | * # Left and Right\r | |
33 | *\r | |
34 | * Because associations are data that span entity types, they are not rightly viewed as\r | |
35 | * "belonging" to either entity. Instead, associations are owned by the `Schema`. Even so,\r | |
36 | * because belonging to an association effects both entities, associations are often\r | |
37 | * viewed from two perspectives or "sides". To distinguish them we call one "left" and the\r | |
38 | * other "right".\r | |
39 | * \r | |
40 | * The reason for this choice derives from {@link Ext.data.schema.ManyToMany many-to-many}\r | |
41 | * associations and their typical underlying "matrix" table. If you were to view the matrix\r | |
42 | * table in a grid, you would see one id on the left and the other id on the right. There\r | |
43 | * is no further significance to these labels.\r | |
44 | * \r | |
45 | * While the concept of left and right might makes sense in a matrix relationship, the\r | |
46 | * labels also apply to the other relationships. In those cases, the "left" entity is the\r | |
47 | * entity that contains the {@link Ext.data.Field#reference} (or foreign key).\r | |
48 | *\r | |
49 | * # Example\r | |
50 | * \r | |
51 | * To help illustrate the various associations, consider a data model with Users, Groups\r | |
52 | * and Organizations. The Users are owned by an Organization. Deleting an Organization,\r | |
53 | * should delete all of the Users it contains. The Users can also be added to one or more\r | |
54 | * Groups, for example, the "moderator" or "admin" Group. Further, a a Level is assigned\r | |
55 | * to each User. Levels represent the subscriber's or customer's rank, such as "premium"\r | |
56 | * or "basic".\r | |
57 | * \r | |
58 | * To summarize:\r | |
59 | * \r | |
60 | * * Users are *{@link Ext.data.schema.ManyToOne many-to-one}* to Organizations\r | |
61 | * * Users are *{@link Ext.data.schema.ManyToOne many-to-one}* to Levels\r | |
62 | * * Users are *{@link Ext.data.schema.ManyToMany many-to-many}* to Groups\r | |
63 | */\r | |
64 | Ext.define('Ext.data.schema.Association', {\r | |
65 | requires: [\r | |
66 | 'Ext.data.schema.Role'\r | |
67 | ],\r | |
68 | \r | |
69 | isOneToOne: false,\r | |
70 | isManyToOne: false,\r | |
71 | isManyToMany: false,\r | |
72 | \r | |
73 | /**\r | |
74 | * @cfg {String} name\r | |
75 | * The name of this association.\r | |
76 | */\r | |
77 | \r | |
78 | /**\r | |
79 | * @property {Object} owner\r | |
80 | * Points at either `left` or `right` objects if one is the owning party in this\r | |
81 | * association or is `null` if there is no owner.\r | |
82 | * @readonly\r | |
83 | */\r | |
84 | owner: null,\r | |
85 | \r | |
86 | /**\r | |
87 | * @property {Ext.Class} definedBy\r | |
88 | * @readonly\r | |
89 | */\r | |
90 | \r | |
91 | /**\r | |
92 | * @property {Ext.data.field.Field} field\r | |
93 | * @readonly\r | |
94 | */\r | |
95 | field: null,\r | |
96 | \r | |
97 | /**\r | |
98 | * @property {Ext.data.schema.Schema} schema\r | |
99 | * @readonly\r | |
100 | */\r | |
101 | \r | |
102 | /**\r | |
103 | * @property {Boolean} nullable\r | |
104 | * @readonly\r | |
105 | */\r | |
106 | \r | |
107 | /**\r | |
108 | * @property {Ext.data.schema.Role} left\r | |
109 | * @readonly\r | |
110 | */\r | |
111 | \r | |
112 | /**\r | |
113 | * @property {Ext.data.schema.Role} right\r | |
114 | * @readonly\r | |
115 | */\r | |
116 | \r | |
117 | constructor: function (config) {\r | |
118 | var me = this,\r | |
119 | left, right;\r | |
120 | \r | |
121 | Ext.apply(me, config);\r | |
122 | \r | |
123 | me.left = left = new me.Left(me, me.left);\r | |
124 | me.right = right = new me.Right(me, me.right);\r | |
125 | \r | |
126 | left.inverse = right;\r | |
127 | right.inverse = left;\r | |
128 | },\r | |
129 | \r | |
130 | hasField: function() {\r | |
131 | return !!this.field; \r | |
132 | },\r | |
133 | \r | |
134 | getFieldName: function() {\r | |
135 | var field = this.field;\r | |
136 | return field ? field.name : '';\r | |
137 | }\r | |
138 | });\r |