2017-03-15 27 views
1

我的風帆多對多協會已經停止保存更改,也就是說,如果我取消選中我的複選框列表中的某個項目,或者選擇一個新項目,則不會保存更改(已填充到連接表)。風帆多對多協會無法保存

它曾經工作,雖然我不知道它被打破了多久。

頁面上的所有其他字段保存正確。

所以我知道它的大部分機制都是正確的,只是多對多的關聯,即更新連接表中的活動記錄列表。

任何提示,我可以在我的保存中做錯嗎?

我有以下型號:

/** 
* User.js 
*/ 

module.exports = { 
    attributes: { 
     projects: { 
      collection: 'project', 
      via: 'users' 
     }, 
    } 

/** 
* Project.js 
*/ 

module.exports = { 

attributes: { 
    users: { 
     collection: 'user', 
     via: 'projects', 
     dominant: true 
    }, 
} 

在我的形式,我返回一個複選框列表像這樣

{ projectname: 'AS Story Database', 
    userlist: [ '10', '3', '1' ], <-- this line is the many to many line from the check boxs 
    projecttype: 'Development', 
    companyid: '1', 
    startdate: 'Sat Jan 01 2011 00:00:00 GMT+1100 (AUS Eastern Daylight Time)', 
    enddate: '' } 
} 

我試圖填充結果集

Project.findOne({'id':id}) 
    .populate('users') <--------- heres the populate I added but didnt seem to have effect 
    .exec(function(err,project){ 

這是我的帆控制器中的保存對話框

var a=req.param('project',null); 
    console.log(a); <-- note this is where the json above is output 
    project.projecttype= a.projecttype, 
    project.projectname= a.projectname, 
    project.companyid= a.companyid, 
    project.users= a.userlist, <-- this is the many to many association that used to work 
    project.startdate = a.startdate, 
    project.enddate = a.enddate 

    project.save(function(err,updated){ <-- here is the save function 
     if (err) { 
      req.session.flash = {'err':err}; 
      sails.controllers.project.edit(req,res); 
     }else{ 
      req.session.flash = {}; 
      res.redirect('project/index'); 
     } 
    }); 
+0

您指定數組'project.users'變量,而不是使用['。新增()' ](http://sailsjs.com/documentation/reference/waterline-orm/populated-values/add)/ ['卸下襬臂()'](http://sailsjs.com/documentation/reference/waterline-orm/填充值/刪除)。它曾經以這種方式工作過嗎? – Sangharsh

+0

是的! 根據文檔,這是它應該工作的方式。 雖然我得出結論,但我需要自己編寫一個深度更新機制。 –

+0

您能否提供指向'project.users'可以直接分配給數組的文檔的鏈接? – Sangharsh

回答

0

@Sangharsh在上面的評論中是正確的;您不能通過爲其分配數組來更新集合,並調用.save()。你可能會想到.update(),它在Sails v0.12.x中允許你提供一個對象數組來替換現有的set(儘管它在Sails 1.0中被刪除了,因爲它引起了很多錯誤和混亂)。

在Sails v0.12.x中更新現有實例集合的正確方法是使用.add().remove()方法。有關更多信息,請參見many-to-many associations doc page

在Sails 1.0中,個別記錄的方法已被刪除以使事情更清晰;您始終使用.addToCollection(),.removeFromCollection().replaceCollection()模型類方法來處理多個關聯。

+0

謝謝,我想我是因爲它實際上似乎起初工作的事實而被誤解爲虛假的安全感。 –

+0

它看起來像.replaceCollection()函數是我想要在這種情況下。 我認爲用數組賦值變量有點簡單,我很驚訝它在第一個實例中工作。 這是工作版本(未經測試,但應該工作): Project.replaceCollection(project.id,'users',a.userlist) –

-1

好的;我終於明白了這一點。 上面的方法(replaceCollection)只適用於較舊的(0.x)版本的sails。

爲了得到它在現代版的工作,使用

<model>.update('id':<the recordID>},{'<association name>':<list of new records>}) 
.exec(function(err,updated){ 
    -do something- 
}); 

即 -

Project.update({'id':id},{'users': a.userlist}).exec(function(err,updated){ 
    project.save(function(err,updated){ 
     //console.log(updated); 
     if (err) { 
      console.log(err); 
      req.session.flash = {'err':err}; 
      sails.controllers.project.edit(req,res); 
     }else{ 
      req.session.flash = {}; 
      res.redirect('project/index'); 
     }; 
    }); 
}); 
+0

這是倒退。在Sails 1.0版本中,'replaceCollection'在Sails <= 0.12.x版本中不可用,'.save()'完全不存在。您可能會在項目中安裝的Sails版本和全局Sails版本之間混淆。 – sgress454

+0

上面的內容應該可以在Sails v0.12.x中使用,但'.save()'是不必要的。它沒有做任何事情。 '.update()'調用可能會奏效,但強烈建議您不要依賴這樣的「嵌套」更新,因爲結果可能會有所不同,具體取決於您在userlist數組中的項目類型(例如,是否它們只是主鍵值或整個對象)。我們鼓勵用'.findOne()。populate()'來檢索項目,然後使用'.users.add()'和'.users.remove()'來操作集合,然後使用'。 save()'來堅持它。 – sgress454