PDA

View Full Version : [CLOSED] foreignKey field overwritten when associated Model instances loaded from nested data



OliverColeman
19 Oct 2011, 6:21 PM
Ext version tested:

Ext 4.0.2

Browser versions tested against:

Chrome 14.0

DOCTYPE tested against:

XHTML 1.0 Strict//EN

Description:
I have a Model that has a self-referential hasMany association (forming a tree structure). The foreignKey field, field_noderef_nid, is an array. When loading a Model instance I include one level of nested data in the server response. When inspecting the Model data in Ext the root Model has the correct values loaded in the field_noderef_nid field (the IDs of the child Model instances that are included in the nested data), however the children Model instances have field_noderef_nid field value overwritten with the ID of the parent/root Model instance rather than an array containing IDs of their own child Model instances (which is what the server response contains). I have verified that the server is sending the correct data.


Steps to reproduce the problem:

Set up the Model definition as below.
Load nested instance data which has the field_noderef_nid field set to an array.

The result that was expected:

The value for the foreignKey field in the nested Model instances to be whatever was sent in the server response data.

The result that occurs instead:

The value for the foreignKey field in the nested Model instances is being overwritten with the ID of the parent Model instance.

Test Case:
The proxy defined in the below Model definition is of course application specific and should be replaced with an appropriate test proxy...


Ext.define('DrupalExt.model.NodePage', {
extend: 'Ext.data.Model',
fields: [
{ name: "field_noderef_nid" },
{ name: "nid" },
],
associations: [
{
associationKey: "noderefNidNodePageList",
foreignKey: "field_noderef_nid",
model: "DrupalExt.model.NodePage",
name: "noderefNidNodePageList",
primaryKey: "nid",
type: "hasMany"
},
],
idProperty: "nid",
proxy: {
type: "rest",
url: "/installations/test/public_html/ext/rest/model/NodePage",
reader: {
type: "json",
root: "nodepage"
},
writer: new Ext.data.JsonWriter()
}
});



Below is an actual server response. Note that the first nested Model data has field_noderef_nid set to [ "27", "37" ], once this data has been turned into an associated Model by Ext the array containing 27 and 37 is replaced with a single scalar value of 12. However the root Model data has field_noderef_nid set to [ "27", "37" ] in the server response and this is not replaced but is kept as an array in the Model instance data.

{
"success": true,
"nodepage": {
"field_noderef_nid": [ "23", "38" ],
"nid": "12",
"noderefNidNodePageList": [
{
"field_noderef_nid": [ "27", "37" ],
"nid": "23"
},
{
"field_noderef_nid": [ ],
"nid": "38"
}
],
}
}





Debugging already done:

none

Possible fix:

not provided

Additional CSS used:

only default ext-all.css

Operating System:

Ubuntu 11.04

evant
19 Oct 2011, 7:41 PM
The foreign key is expected to refer to the value of the parent, it needs to maintain that relationship. Why are you using the same key for different purposes?

OliverColeman
19 Oct 2011, 8:22 PM
Ahh, I misread the docs, my apologies.

I suppose it is configured this way to avoid having fields that are arrays of child IDs...