Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / docs / api / Ext.data.Association.html
1 <!DOCTYPE html><html><head><title>Ext.data.Association | Ext JS 4.0 Documentation</title><script type="text/javascript" src="../ext-all.js"></script><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../scrollbars.css" type="text/css"><link rel="stylesheet" href="../docs.css" type="text/css"><link id="styleCss" rel="stylesheet" href="../style.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script><link rel="stylesheet" href="../prettify.css" type="text/css"><!-- link(rel: 'stylesheet', href: req.baseURL + '/css/ext4.css', type: 'text/css')--><link rel="shortcut icon" type="image/ico" href="../favicon.ico"><!--[if IE]>
2 <style type="text/css">.head-band { display: none; }
3 .header { border: 0; top: 0; left: 0px; background: url(../header.gif) repeat-x; }
4 .doc-tab .members .member a.more { background-color: #efefef; }
5 </style><link rel="stylesheet" href="/new/css/ie.css" type="text/css"><![endif]-->
6 </head><body id="ext-body" class="iScroll"><div id="notice" class="notice">For up to date documentation and features, visit 
7 <a href="http://docs.sencha.com/ext-js/4-0">http://docs.sencha.com/ext-js/4-0</a></div><div class="wrapper"><div class="head-band"></div><div class="header"><h2><a href="../index.html">Sencha Documentation</a></h2></div><div id="search"><form><input type="text" placeholder="Search" id="search-field" autocomplete="off" name="q"></form><div id="search-box"></div></div><div id="treePanel"></div><div id="container"><script type="text/javascript">
8
9     req = {
10         liveURL: '.',
11         standAloneMode: true,
12         origDocClass: 'Ext.data.Association',
13         docClass: 'Ext.data.Association',
14         docReq: 'Ext.data.Association',
15         version: '4.0',
16         baseURL: '.',
17         baseDocURL: '.',
18         baseProdURL: '.'
19     };
20
21     clsInfo = {};
22
23
24
25 </script>
26
27 <script type="text/javascript" src="../search.js"></script>
28 <!--script type="text/javascript" src="/new/javascripts/app/examples.js"></script-->
29 <script type="text/javascript" src="../class_tree.js"></script>
30 <script type="text/javascript" src="../class_doc.js"></script>
31 <script type="text/javascript">
32     req.source = 'Association.html#Ext-data.Association';
33     clsInfo = {"methods":["Association","getReader"],"cfgs":["associatedModel","associationKey","ownerModel","primaryKey","reader"],"properties":["associatedName","ownerName"],"events":[],"subclasses":["Ext.data.BelongsToAssociation","Ext.data.HasManyAssociation"]};
34     Ext.onReady(function() {
35         Ext.create('Docs.classPanel');
36     });
37 </script><div id="top-block" class="top-block"><h1 id="clsTitle" class="cls"><a href="../source/Association.html#Ext-data.Association" target="_blank">Ext.data.Association</a></h1></div><div id="docContent"><div id="doc-overview-content"><div class="lft"><p>Associations enable you to express relationships between different <a href="Ext.data.Model.html" rel="Ext.data.Model" class="docClass">Models</a>. Let's say we're
38 writing an ecommerce system where Users can make Orders - there's a relationship between these Models that we can
39 express like this:</p>
40
41
42
43
44 <pre class="prettyprint"><code>Ext.define('User', {
45     extend: 'Ext.data.Model',
46     fields: ['id', 'name', 'email'],
47
48     hasMany: {model: 'Order', name: 'orders'}
49 });
50
51 Ext.define('Order', {
52     extend: 'Ext.data.Model',
53     fields: ['id', 'user_id', 'status', 'price'],
54
55     belongsTo: 'User'
56 });
57 </code></pre>
58
59
60
61
62 <p>We've set up two models - User and Order - and told them about each other. You can set up as many associations on
63 each Model as you need using the two default types - <a href="Ext.data.HasManyAssociation.html" rel="Ext.data.HasManyAssociation" class="docClass">hasMany</a> and
64 <a href="Ext.data.BelongsToAssociation.html" rel="Ext.data.BelongsToAssociation" class="docClass">belongsTo</a>. There's much more detail on the usage of each of those inside their
65 documentation pages. If you're not familiar with Models already, <a href="Ext.data.Model.html" rel="Ext.data.Model" class="docClass">there is plenty on those too</a>.</p>
66
67
68
69
70 <p><u>Further Reading</u></p>
71
72
73
74
75 <ul style="list-style-type: disc; padding-left: 20px;">
76   <li><a href="Ext.data.HasManyAssociation.html" rel="Ext.data.HasManyAssociation" class="docClass">hasMany associations</a>
77   <li><a href="Ext.data.BelongsToAssociation.html" rel="Ext.data.BelongsToAssociation" class="docClass">belongsTo associations</a>
78   <li><a href="Ext.data.Model.html" rel="Ext.data.Model" class="docClass">using Models</a>
79 </ul>
80
81
82 <p><b>Self association models</b></p>
83
84 <p>We can also have models that create parent/child associations between the same type. Below is an example, where
85 groups can be nested inside other groups:</p>
86
87
88 <pre class="prettyprint"><code>
89 // Server Data
90 {
91     "groups": {
92         "id": 10,
93         "parent_id": 100,
94         "name": "Main Group",
95         "parent_group": {
96             "id": 100,
97             "parent_id": null,
98             "name": "Parent Group"
99         },
100         "child_groups": [{
101             "id": 2,
102             "parent_id": 10,
103             "name": "Child Group 1"
104         },{
105             "id": 3,
106             "parent_id": 10,
107             "name": "Child Group 2"
108         },{
109             "id": 4,
110             "parent_id": 10,
111             "name": "Child Group 3"
112         }]
113     }
114 }
115
116 // Client code
117 Ext.define('Group', {
118     extend: 'Ext.data.Model',
119     fields: ['id', 'parent_id', 'name'],
120     proxy: {
121         type: 'ajax',
122         url: 'data.json',
123         reader: {
124             type: 'json',
125             root: 'groups'
126         }
127     },
128     associations: [{
129         type: 'hasMany',
130         model: 'Group',
131         primaryKey: 'id',
132         foreignKey: 'parent_id',
133         autoLoad: true,
134         associationKey: 'child_groups' // read child data from child_groups
135     }, {
136         type: 'belongsTo',
137         model: 'Group',
138         primaryKey: 'id',
139         foreignKey: 'parent_id',
140         autoLoad: true,
141         associationKey: 'parent_group' // read parent data from parent_group
142     }]
143 });
144
145
146 Ext.onReady(function(){
147     
148     Group.load(10, {
149         success: function(group){
150             console.log(group.getGroup().get('name'));
151             
152             group.groups().each(function(rec){
153                 console.log(rec.get('name'));
154             });
155         }
156     });
157     
158 });
159 </code></pre>
160
161 <div class="members"><div class="m-cfgs"><div class="definedBy">Defined By</div><a name="configs"></a><h3 class="cfg p">Config Options</h3><h4 class="cfgGroup">Other Configs</h4><div id="config-associatedModel" class="member f ni"><a href="Ext.data.Association.html#config-associatedModel" rel="config-associatedModel" class="expand more"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.data.Association.html" class="definedIn docClass">Ext.data.Association</a><br/><a href="../source/Association.html#Ext-data.Association-cfg-associatedModel" class="viewSource">view source</a></div><a name="associatedModel"></a><a name="config-associatedModel"></a><a href="Ext.data.Association.html#" rel="config-associatedModel" class="cls expand">associatedModel</a><span> : String</span></div><div class="description"><div class="short"><p>The string name of the model that is being associated with. Required</p>
162 </div><div class="long"><p>The string name of the model that is being associated with. Required</p>
163 </div></div></div><div id="config-associationKey" class="member ni"><a href="Ext.data.Association.html#config-associationKey" rel="config-associationKey" class="expand more"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.data.Association.html" class="definedIn docClass">Ext.data.Association</a><br/><a href="../source/Association.html#Ext-data.Association-cfg-associationKey" class="viewSource">view source</a></div><a name="associationKey"></a><a name="config-associationKey"></a><a href="Ext.data.Association.html#" rel="config-associationKey" class="cls expand">associationKey</a><span> : String</span></div><div class="description"><div class="short"><p>The name of the property in the data to read the association from.
164 Defaults to the name of the associated model.</p>
165 </div><div class="long"><p>The name of the property in the data to read the association from.
166 Defaults to the name of the associated model.</p>
167 </div></div></div><div id="config-ownerModel" class="member ni"><a href="Ext.data.Association.html#config-ownerModel" rel="config-ownerModel" class="expand more"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.data.Association.html" class="definedIn docClass">Ext.data.Association</a><br/><a href="../source/Association.html#Ext-data.Association-cfg-ownerModel" class="viewSource">view source</a></div><a name="ownerModel"></a><a name="config-ownerModel"></a><a href="Ext.data.Association.html#" rel="config-ownerModel" class="cls expand">ownerModel</a><span> : String</span></div><div class="description"><div class="short"><p>The string name of the model that owns the association. Required</p>
168 </div><div class="long"><p>The string name of the model that owns the association. Required</p>
169 </div></div></div><div id="config-primaryKey" class="member ni"><a href="Ext.data.Association.html#config-primaryKey" rel="config-primaryKey" class="expand more ar"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.data.Association.html" class="definedIn docClass">Ext.data.Association</a><br/><a href="../source/Association.html#Ext-data.Association-cfg-primaryKey" class="viewSource">view source</a></div><a name="primaryKey"></a><a name="config-primaryKey"></a><a href="Ext.data.Association.html#" rel="config-primaryKey" class="cls expand">primaryKey</a><span> : String</span></div><div class="description"><div class="short">The name of the primary key on the associated model. Defaults to 'id'.
170 In general this will be the Ext.data.Model.idP...</div><div class="long"><p>The name of the primary key on the associated model. Defaults to 'id'.
171 In general this will be the <a href="Ext.data.Model.html#idProperty" rel="Ext.data.Model#idProperty" class="docClass">Ext.data.Model.idProperty</a> of the Model.</p>
172 </div></div></div><div id="config-reader" class="member ni"><a href="Ext.data.Association.html#config-reader" rel="config-reader" class="expand more"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.data.Association.html" class="definedIn docClass">Ext.data.Association</a><br/><a href="../source/Association.html#Ext-data.Association-cfg-reader" class="viewSource">view source</a></div><a name="reader"></a><a name="config-reader"></a><a href="Ext.data.Association.html#" rel="config-reader" class="cls expand">reader</a><span> : Ext.data.reader.Reader</span></div><div class="description"><div class="short"><p>A special reader to read associated data</p>
173 </div><div class="long"><p>A special reader to read associated data</p>
174 </div></div></div></div><div class="m-properties"><a name="properties"></a><div class="definedBy">Defined By</div><h3 class="prp p">Properties</h3><div id="property-associatedName" class="member f ni"><a href="Ext.data.Association.html#property-associatedName" rel="property-associatedName" class="expand more"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.data.Association.html" class="definedIn docClass">Ext.data.Association</a><br/><a href="../source/Association.html#Ext-data.Association-property-associatedName" class="viewSource">view source</a></div><a name="associatedName"></a><a name="property-associatedName"></a><a href="Ext.data.Association.html#" rel="property-associatedName" class="cls expand">associatedName</a><span> : String</span></div><div class="description"><div class="short"><p>The name of the model is on the other end of the association (e.g. if a User model hasMany Orders, this is 'Order')</p>
175 </div><div class="long"><p>The name of the model is on the other end of the association (e.g. if a User model hasMany Orders, this is 'Order')</p>
176 </div></div></div><div id="property-ownerName" class="member ni"><a href="Ext.data.Association.html#property-ownerName" rel="property-ownerName" class="expand more"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.data.Association.html" class="definedIn docClass">Ext.data.Association</a><br/><a href="../source/Association.html#Ext-data.Association-property-ownerName" class="viewSource">view source</a></div><a name="ownerName"></a><a name="property-ownerName"></a><a href="Ext.data.Association.html#" rel="property-ownerName" class="cls expand">ownerName</a><span> : String</span></div><div class="description"><div class="short"><p>The name of the model that 'owns' the association</p>
177 </div><div class="long"><p>The name of the model that 'owns' the association</p>
178 </div></div></div></div><div class="m-methods"><a name="methods"></a><div class="definedBy">Defined By</div><h3 class="mth p">Methods</h3><div id="method-Association" class="member f ni"><a href="Ext.data.Association.html#method-Association" rel="method-Association" class="expand more ar"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.data.Association.html" class="definedIn docClass">Ext.data.Association</a><br/><a href="../source/Association.html#Ext-data.Association-method-constructor" class="viewSource">view source</a></div><a name="Association"></a><a name="method-Association"></a><a href="Ext.data.Association.html#" rel="method-Association" class="cls expand">Association</a>(
179 <span class="pre">Object config</span>)
180  : void</div><div class="description"><div class="short"><p>&nbsp;</p></div><div class="long">
181 <h3 class="pa">Parameters</h3><ul><li><span class="pre">config</span> : Object<div class="sub-desc"><p>Optional config object</p>
182 </div></li></ul><h3 class="pa">Returns</h3><ul><li><span class="pre">void</span>&nbsp; &nbsp;
183 </li></ul></div></div></div><div id="method-getReader" class="member ni"><a href="Ext.data.Association.html#method-getReader" rel="method-getReader" class="expand more ar"><span>&nbsp;</span></a><div class="title"><div class="meta"><a href="Ext.data.Association.html" class="definedIn docClass">Ext.data.Association</a><br/><a href="../source/Association.html#Ext-data.Association-method-getReader" class="viewSource">view source</a></div><a name="getReader"></a><a name="method-getReader"></a><a href="Ext.data.Association.html#" rel="method-getReader" class="cls expand">getReader</a> : Ext.data.reader.Reader</div><div class="description"><div class="short"><p>Get a specialized reader for reading associated data</p>
184 </div><div class="long"><p>Get a specialized reader for reading associated data</p>
185 <h3 class="pa">Returns</h3><ul><li><span class="pre">Ext.data.reader.Reader</span>&nbsp; &nbsp;<p>The reader, null if not supplied</p>
186 </li></ul></div></div></div></div></div></div></div><div id="pageContent"></div></div></div></div></body></html>