Question

I have the model:

 Ext.define('EvaluateIt.model.Address', {
    extend: 'Ext.data.Model',
    config: {
        idProperty: 'id', 
        fields: [
            {name: 'address', type: 'string'},
            {name: 'city', type: 'string'},
            {name: 'state', type: 'string'},
            {name: 'zipcode', type: 'string'},
            {name: 'county', type: 'string'}
        ],
        proxy: {
            type: "sql", 
            database: 'Test'
        }

    }
});

In my controller, I insert data into this model as follows:

 var address = Ext.create('EvaluateIt.model.Address', {
     address: json[i].garden.address.address
  });
 address.save();

Where the json array is grabbed via Ajax and thus inserted into the model without any problems.

However, when I try to access the id from the model like,

console.log('address.id ' + address.id);

I get something of the form ext-record-n (where n does not even map to the id in my Address table). How can I reference the actual value of the id column in this table? I tried this, but was unsuccessful: https://github.com/tomalex0/SenchaTouch-v2-SqliteProxy/issues/3

Was it helpful?

Solution

Adding the id field to the fields array will cause the proxy to create the database table with column id as an AUTOINCREMENT field.

Ext.define('EvaluateIt.model.Address', {
    extend: 'Ext.data.Model',
    config: {
        idProperty: 'id', 
        fields: [
            {name: 'id', type: 'int'},
            {name: 'address', type: 'string'},
            {name: 'city', type: 'string'},
            {name: 'state', type: 'string'},
            {name: 'zipcode', type: 'string'},
            {name: 'county', type: 'string'}
        ],
        proxy: {
            type: "sql", 
            database: 'Test'
        }

    }
});

The proxy will read the id value created by the database. You can access the value by passing a callback function to the model save method.

var address = Ext.create('EvaluateIt.model.Address', {
     address: json[i].garden.address.address
});

address.save(function(record) {
    console.log('address.id ' + record.getId();
}, this);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top