var dataStore = Backendless.Persistence.of(Classes.Notice);
var removeObj=dataStore.findById(id);
var childrens = dataStore.loadRelations(removeObj, ["Notice", "Notice.noticeRelUserId"] );
dataStore.remove(id);
var dataStoreChild = Backendless.Persistence.of(Classes.noticeRelUserId);
dataStoreChild.remove(childrens,new Backendless.Async(function (obj){
notify("success",GetNotices);
}));
var dataStore = Backendless.Persistence.of(Classes.Notice);
var removeObj=dataStore.findById(id);
var childrens=removeObj.getNoticeRelUserId();
removeObj.removeAllNoticeRelUserId();
var child=Backendless.Persistence.of(Classes.noticeRelUserId);
if(childrens.length>0)
{
for(var i=0;i<childrens.length;i++)
{
child.remove(childrens.objectId);
}
}
dataStore.remove(id).then(function () {
notify("success",GetNotices);
}).catch(gotError);
Cycle delete, right?
if I have correct understanding you need to delete object and all children of it?
To do this I suggest make it in two steps:
- remove all related object
- remove object
To remove all related object you can use remove bulk functionality. Unfortanly JS sdk has no implementation of this fiture, but there is REST route to do this
For example you have table ‘Post’, with ‘rel’ column which is relation to table ‘tt2’.
To remove all relations of Post you have to call bulk remove on table ‘tt2’ with this query
Post[rel].objectId = '5A93F450-0734-D0E0-FFD9-1A6E0592EC00'
where objectId is an object from table Post
example of curl query would be:
curl -X DELETE -H 'application-id : <appId>' -H 'secret-key : <secret-key>' https://api.backendless.com/v1/data/bulk/tt2?where=Post%5Brel%5D.objectId%20%3D%20%275A93F450-0734-D0E0-FFD9-1A6E0592EC00%27
after this you just delete an object from table ‘Post’
var dataStore = Backendless.Persistence.of(Classes.Notice);
var removeObj=dataStore.findById(id);
removeObj.removeAllNoticeRelUserId();
var noticeRel=Backendless.Persistence.of(Classes.noticeRelUserId);
noticeRel.findById(id).then(function (rel) {
console.log(rel);
rel.removeAllUserId();
noticeRel.remove(rel.objectId);
});
dataStore.remove(id,new Backendless.Async(function (savedObject){ //删除notice表
notify("success",GetNotices);
}));
Is this all right? REST
Not many to many relationships?