2014-10-07 60 views
7

Sails.js 0.10 RC8Sails.js同型號多對多關聯

我已經完全用完的想法,這

我有一個模型叫用戶,我想它的關聯收集到其他用戶,如朋友列表。像許多-to-many關聯

//User.js 
    friends: { 
    collection: 'user', 
    via: 'friends' 
    } 

但是當我運行 .populate('friends') 它不填充任何內容。有任何想法嗎?

+0

你有沒有找到這個解決方案?我目前正在嘗試做同樣的事情。 – Michael 2015-02-23 04:47:19

+1

嗨邁克爾,這是問題,因爲它是相同的模型,這意味着他們都有一個多對多的關聯。水線自動創建連接表,但它佔據了主導地位。所以你不能使用它的填充函數。我發現的最好方法是創建另一個模型(例如Friends.js),並創建一個用戶模型,然後創建其他用戶模型集合。然後,我設置了一個「填充」的鉤子,當我需要它時。 – scott 2015-02-23 15:12:11

+1

謝謝斯科特!這正是我要去的地方,但很高興聽到它真的爲你工作。我會繼續朝着這條道路前進。 – Michael 2015-02-24 03:23:52

回答

1

我發現實現這個最好的方法實際上是增加了對id的引用。

檢查這個用戶模型:

module.exports = { 
    attributes: { 
    name: { 
     type: 'string', 
     required: true, 
     minLength: 2 
    }, 
    email: { 
     type: 'email', 
     required: true, 
     unique: true 
    }, 
    friends: { 
     collection: 'user', 
     via: 'id' 
    } 
    } 
}; 

現在,如果你運行sails console您可以測試下面的命令:

User.create({name:'test',email:'[email protected]'}).exec(console.log); 

它返回:

{ name: 'test', 
    email: '[email protected]', 
    createdAt: '2016-12-01T22:06:19.723Z', 
    updatedAt: '2016-12-01T22:06:19.723Z', 
    id: 1 } 

您創建了第一個用戶。讓我們創建一些其他的:

User.create({name:'test2',email:'[email protected]'}).exec(console.log); 

結果造成:

{ name: 'test2', 
    email: '[email protected]', 
    createdAt: '2016-12-01T22:06:40.808Z', 
    updatedAt: '2016-12-01T22:06:40.808Z', 
    id: 2 } 

讓我們看看我們至今:

User.find().populate('friends').exec(console.log); 

[ { friends: [], 
    name: 'test', 
    email: '[email protected]', 
    createdAt: '2016-12-01T22:06:19.723Z', 
    updatedAt: '2016-12-01T22:06:19.723Z', 
    id: 1 }, 
    { friends: [], 
    name: 'test2', 
    email: '[email protected]', 
    createdAt: '2016-12-01T22:06:40.808Z', 
    updatedAt: '2016-12-01T22:06:40.808Z', 
    id: 2 } ] 

現在,讓我們創建一個用戶與朋友,使用引用第一個用戶。請注意,我只傳遞一個ID,而不是數組:

User.create({name:'test3',email:'[email protected]', friends:1}).exec(console.log); 

現在,結果是這一個。請注意,「朋友」沒有出現。這是設計。

{ name: 'test3', 
    email: '[email protected]', 
    createdAt: '2016-12-01T22:07:34.988Z', 
    updatedAt: '2016-12-01T22:07:34.994Z', 
    id: 3 } 

讓我們做一個找到populate查看當前狀態:

User.find().populate('friends').exec(console.log); 

現在我們看到的是,第三用戶朋友。其他人有一個空陣列。

[ { friends: [], 
    name: 'test', 
    email: '[email protected]', 
    createdAt: '2016-12-01T22:06:19.723Z', 
    updatedAt: '2016-12-01T22:06:19.723Z', 
    id: 1 }, 
    { friends: [], 
    name: 'test2', 
    email: '[email protected]', 
    createdAt: '2016-12-01T22:06:40.808Z', 
    updatedAt: '2016-12-01T22:06:40.808Z', 
    id: 2 }, 
    { friends: 
    [ { name: 'test', 
     email: '[email protected]', 
     createdAt: '2016-12-01T22:06:19.723Z', 
     updatedAt: '2016-12-01T22:06:19.723Z', 
     id: 1 } ], 
    name: 'test3', 
    email: '[email protected]', 
    createdAt: '2016-12-01T22:07:34.988Z', 
    updatedAt: '2016-12-01T22:07:34.994Z', 
    id: 3 } ] 

讓我們創建一個第四名,這時候有兩個朋友:

User.create({name:'test4',email:'[email protected]', friends:[1,2]}).exec(console.log); 

,導致(再次,沒有朋友屬性):

{ name: 'test4', 
    email: '[email protected]', 
    createdAt: '2016-12-01T22:07:50.539Z', 
    updatedAt: '2016-12-01T22:07:50.542Z', 
    id: 4 } 

這一次,我們通過一個數組的ID爲friends。讓我們看看目前的狀態:

​​

現在,你如何添加一個朋友到現有的用戶?這有點奇怪:你必須先用find這個用戶,然後在產生的模型上用add函數,然後,save它。在我的日常代碼中,我有一個輔助函數可以做到這一點。因此,下面是示例:

User.findOne(1).populate('friends').exec(function(err,u){ u.friends.add(3);u.save(function(err){ if(err) console.error(err);});}); 

現在在控制檯中我們看不到任何結果。 現在讓我們來檢查用戶內容:

User.find().populate('friends').exec(console.log); 

[ { friends: 
    [ { name: 'test3', 
     email: '[email protected]', 
     createdAt: '2016-12-01T22:07:34.988Z', 
     updatedAt: '2016-12-01T22:07:34.994Z', 
     id: 3 } ], 
    name: 'test', 
    email: '[email protected]', 
    createdAt: '2016-12-01T22:06:19.723Z', 
    updatedAt: '2016-12-01T22:09:41.410Z', 
    id: 1 }, 
    { friends: [], 
    name: 'test2', 
    email: '[email protected]', 
    createdAt: '2016-12-01T22:06:40.808Z', 
    updatedAt: '2016-12-01T22:06:40.808Z', 
    id: 2 }, 
    { friends: 
    [ { name: 'test', 
     email: '[email protected]', 
     createdAt: '2016-12-01T22:06:19.723Z', 
     updatedAt: '2016-12-01T22:09:41.410Z', 
     id: 1 } ], 
    name: 'test3', 
    email: '[email protected]', 
    createdAt: '2016-12-01T22:07:34.988Z', 
    updatedAt: '2016-12-01T22:07:34.994Z', 
    id: 3 }, 
    { friends: 
    [ { name: 'test', 
     email: '[email protected]', 
     createdAt: '2016-12-01T22:06:19.723Z', 
     updatedAt: '2016-12-01T22:09:41.410Z', 
     id: 1 }, 
     { name: 'test2', 
     email: '[email protected]', 
     createdAt: '2016-12-01T22:06:40.808Z', 
     updatedAt: '2016-12-01T22:06:40.808Z', 
     id: 2 } ], 
    name: 'test4', 
    email: '[email protected]', 
    createdAt: '2016-12-01T22:07:50.539Z', 
    updatedAt: '2016-12-01T22:07:50.542Z', 
    id: 4 } ] 

使用這種方法,您可以在同一用戶甚至添加到友人收藏!

User.findOne(1).populate('friends').exec(function(err,u){ u.friends.add(1);u.save(function(err){ if(err) console.error(err);});}); 

玩得開心!

0

您應該使用的.populate('friends')代替.populate(friends)

+0

我真的很感激你的回覆。當我的意思是.populate('朋友'),但它不起作用:-( – scott 2014-10-08 04:46:42

+1

@scott試試看:收集:'用戶', – haotang 2014-10-08 07:33:58

2

你的模型應該是這樣的......

//User.js 
friends: { 
    collection: 'friend', 
    via: 'user' 
} 

//Friend.js 
user: { 
    model: 'user' 
    via: 'friends' 
} 

而且航行10 RC8是舊的。你應該在航行10.5

0

事實證明,當你在水線上有多對多的關聯時,你必須聲明其中一個模型爲主導。由於它們是相同的模型,所以都不佔主導地位。在創建連接表時,無法填充它。

+0

Dominant用於跨適配器查詢,而不是你描述的。http: //sailsjs.com/documentation/concepts/models-and-orm/associations/dominance它只是說幫助程序表將在何處創建 – 2016-12-01 01:03:27

+0

當您在多對多關聯中設置主導權時,您是正確的,它確實會說其中連接表將在發生跨適配器查詢時發生,但它也會影響連接表的命名順序正常情況'user_friends__friend_users'這些名稱很重要,因爲它們將是模型的名稱和屬性。 ,這些不能在同一個模型中(參見OP註釋)。因爲水線錯誤,連接表出錯了。如果我回想起來,連接表名稱以user_friends__users_friends或類似'user'這個表的名稱出現不存在。 – scott 2016-12-01 18:41:06