0

I have a Person model which has an Address associated to it via 'hasOne'. Both models load data via REST proxy. If I load a person and try to get the address I can see a http request to the url of the addresses but without any kind of person id in it so my server sends an error.

Person:

Ext.define('AP.model.Person', {
    extend: 'Ext.data.Model',
    fields: [
        { name: 'id', type: 'int', persist : false },
        { name: 'name', type: 'string' }
    ],
    proxy: {
        type: 'rest',
        url : AP_ROOT_URL + 'persons/'
    },
    associations: [{
        type: 'hasOne', 
        model: 'AP.model.Addresses', 
        foreignKey: 'person_id',
        primaryKey: 'id',
        getterName: "getAddress"
    }]
});

Address:

Ext.define('AP.model.Address', {
    extend: 'Ext.data.Model',
    fields: [
        { name: 'person_id', type: 'int', persist : false },
        { name: 'address', type: 'string' }
    ],
    proxy: {
        type: 'rest',
        url : AP_ROOT_URL + 'addresses/'
    }
});

The following code:

person.getAddress(function(address){
    console.log(address);
});

creates a request to: http://localhost/addresses

when it should create a request to: http://localhost/addresses/person_id (http://localhost/addresses/1)

4

1 回答 1

0

What you have here is a HasMany relation, not HasOne. You won't get HasOne to work with these models because, as said in the docs: "the owner model is expected to have a foreign key which references the primary key of the associated model". In your case, the foreign key is in the associated model (which makes it a 1-n relationship).

于 2013-09-25T07:51:08.583 回答