PDA

View Full Version : Datastore create new does not receive id problem



Balduran
4 Jul 2012, 5:38 AM
Hi

I am quite new to ext js and have run into a problem.
I have a very simple user grid from where you can create new user entry to datastore. Everything seems to work, create query is sent to database, but after that there is a "user" in store that id is null.

Create query json:
{"data":[{"id":null,"name":"test","email":"test@test.ee"}]}
Create query response is:
{"data":[{"id":9}],"success":true}
(have tried different variations, like data is single element, returning all fields etc, but nothing has worked).
I think ext js should handle this automatically, that record id gets updated after create response is received, but I can't seem to get it working.

So the data store:

Ext.define('AM.store.Users', {
extend: 'Ext.data.Store',
model: 'AM.model.User',
autoLoad: true,
autoSync: true,
pageSize: 4,
proxy: {
type: 'ajax',
api: {
create: 'data/users.php?action=create',
read: 'data/users.php?action=read',
update: 'data/users.php?action=update',
destroy: 'data/users.php?action=delete'
},
reader: {
type: 'json',
root: 'users',
successProperty: 'success',
totalProperty: 'total',
},
writer: {
type: 'json',
root: 'data',
writeAllFields: false,
allowSingle: false,
}
}
});


I tried debugging through the extjs-all-debug, but it got a bit too comlicated to understand where this event could be...

Please help :)

Izhaki
4 Jul 2012, 6:57 AM
It should update automatically if the server returns the record sent with the updated id.

This code works for me:


function Create( $aRecord )
{
$iInsertClause = InsertClause::FromObject( $aRecord, self::$persistents );

sql( "INSERT INTO Users $iInsertClause" );


// Return the record with the newly inserted record id
$aRecord->id = mysql_insert_id();
return array(
'success' => true,
'data' => $aRecord
);
}


And the store definition:



Ext.define('BS.store.Users', {


extend: 'Ext.data.Store',
model: 'BS.model.User',


autoSync: true,
autoLoad: true,


proxy: {
type: 'direct',
api: {
create: Users.Create,
read: Users.Read,
update: Users.Update,
destroy: Users.Destroy,

},
reader: {
type: 'json',
root: 'data',
},
},

});

Looking at your code, the root of the reader and writer is different, and the reader might expect a root data on 'users' while your response has a root 'data'. This could be the issue.

If not, could you please provide your model definition?