ExtJS存储外键被重写为零

时间:2014-03-07 09:49:59

标签: javascript extjs foreign-keys model-associations

我有一个ExtJS应用程序,有两个模型(和相应的商店):Salary和Employee。 薪水对员工有外键。

当加载Salary时,外键被覆盖为0,为什么会这样,我该如何防止呢?

在控制台中:

> 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}

薪资模式:

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
        }
    }
});

薪资店:

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
});

当我按照建议将代理移动到模型时,它不起作用(没有为此代理指定直接函数) - 但这是另一个问题。

1 个答案:

答案 0 :(得分:0)

模型字段定义中存在问题。对于mapping属性:

不要使用数据库表的名称,

但是真实关联的名称:employee

真的,改为:

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

修好了,但我不确切知道为什么。 (一些ExtJS魔术?)