2014-11-03 67 views
0

现在我用这个代码保存新/存在的记录:煎茶触摸保存新的记录和更新ID(键)

var values = this.getFrmDetails().getValues(); 
var record = this.getFrmDetails().getRecord(); 
var store = Ext.getStore('usersStore'); 
record.set(values); 
// Is this a new user? 
if (record.data.id==-1) 
{ 
    record.save(); 
    store.add(record); 
} 
store.load(); 
this.getMainView().pop(); 
  • 此代码工作正常,但它总是保存&负荷全数据再次,我跟踪记录由id所以当我添加新记录我通过:-1为id。

  • 如果不使用store.load(),则直到我重新加载我的应用程序时,该ID将始终为-1

如何用服务器端创建的id更新新记录?

  • 我使用REST代理与PHP服务器端MySQL数据库。

  • 顺便说一句,在Firebug中,我总是看到PUT新的/更新的记录。我如何才能让Sencha touch只为新记录发送POST而仅需更新现有帐户PUT

  • 我有autosync=true上的商店。

谢谢

回答

0

我使用C#,而不是PHP,但是我用的是相同的“-1 '技术。当我在商店中插入记录并与远程同步时,我会返回插入的记录,并且商店从服务响应中获取值。

var store = Ext.getStore('fooStore'); 
var newFoo = { 
    a: 'a', 
    b: 'b' 
}; 

store.add(newFoo) // here the record id would be -1 
store.sync({ 
    callback: function() { 
     // here the id is the returned from the server 
     // this is not needed if you have autoSync, It's just for clearness 
    } 
}); 

而且我的C#代码的结构将是:

function Foo Insert(Foo foo) { 
    var myTable = new MyTable(); // In PHP would be different, but I guess that the idea is clear. 
    var newRecord = myTable.Insert(foo); // The insert method returns the added record. 
    return newRecord; 
} 

则返回的记录将通过模型或商店定义和ID采取将自动更新。如果您使用此方法,则不需要使用store.load()方法,因为响应具有所需的更新信息。

的那种请求POST而不是PUT的,你有你的型号/存储代理定义中使用这样的:

// ... 
proxy: { 
    // ... 
    actionMethods: { 
     create: 'POST', 
     read: 'POST', 
     update: 'POST', 
     destroy: 'POST' 
    } 
    // ... 
} 
// ... 
0

我找到了一个解决方案:

var newid=-1; 
record.save({success : function(res){newid=res.getId();}}); 
record.data.id=newid; 

PHP端:

$sql="SELECT LAST_INSERT_ID() id"; 
    $rs=mysql_query($sql); 
    $db_field = mysql_fetch_assoc($rs); 
    echo json_encode(array("success"=>$db_field['id']));