Question

I have an ExtJS application with two models (and corresponding stores): Salary and Employee. Salary has a foreign key to Employee.

When the Salary is loaded, the foreign key is overriden to 0, why is that and how can I prevent it?

In the console:

> s = Ext.getStore('crm.Salaries')
> s.data.items[0].raw
Object {id: 1, date: "2014-03-05 00:00:00", amount: 1000, employee: 6982}
> s.data.items[0].data
Object {id: 1, date: Wed Mar 05 2014 00:00:00 GMT+0100 (CET), amount: 1000, employee_id: 0}

Salary model:

Ext.define('Project.model.crm.Salary', {
    extend: 'Ext.data.Model',


    fields: [{
        name: 'id'
    },{
        name: 'date',
        type: 'date',
        dateFormat: Ext.Date.patterns.ISO8601Long
    },{
        name: 'amount',
        type: 'float'
    },{
        name: 'employee_id',
        mapping: 'user',
        type: 'int'
    }],

    syncAssociations: [{
        store: 'Employees', key: 'employee_id'
    }],

    statics: {
        instanceDefaults: {
            amount: 0.0
        }
    }
});

Salary store:

Ext.define('Project.store.crm.Salaries', {
    extend: 'Project.lib.base.Store',
    model: 'Project.model.crm.Salary',

    proxy: {
        type: 'direct',
        api: {
            read: $["crmModule.SalaryController"].list
        }
    },

    requires: ['Project.store.Employees'],
    autoLoad: false,
    remoteFilter: true
});

When I move proxy to the model as I got suggested, it does not work (No direct function specified for this proxy) - but that's another issue.

Was it helpful?

Solution

There is a problem in the model field definitions. For the mapping attribute:

do not use the name of the database table,

but the name of the real assotiation: employee.

Really, changing to:

{
    name: 'employee_id',
    mapping: 'employee',
    type: 'int'
}

fixed it, but I don't know exactly why. ( some ExtJS magic? )

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top