Tôi thấy rằng cách tốt nhất để thực hiện điều này thực sự là thêm tham chiếu đến số id
.
Kiểm tra này mô hình tài:
module.exports = {
attributes: {
name: {
type: 'string',
required: true,
minLength: 2
},
email: {
type: 'email',
required: true,
unique: true
},
friends: {
collection: 'user',
via: 'id'
}
}
};
Bây giờ, nếu bạn chạy sails console
bạn có thể kiểm tra các lệnh sau:
User.create({name:'test',email:'[email protected]'}).exec(console.log);
Nó trả về:
{ name: 'test',
email: '[email protected]',
createdAt: '2016-12-01T22:06:19.723Z',
updatedAt: '2016-12-01T22:06:19.723Z',
id: 1 }
Bạn tạo của bạn đầu tiên người dùng. Cho phép tạo ra một số những người khác:
User.create({name:'test2',email:'[email protected]'}).exec(console.log);
Hệ quả là:
{ name: 'test2',
email: '[email protected]',
createdAt: '2016-12-01T22:06:40.808Z',
updatedAt: '2016-12-01T22:06:40.808Z',
id: 2 }
Hãy xem những gì chúng tôi có cho đến nay:
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 } ]
Bây giờ, chúng ta hãy tạo một người dùng với một người bạn, bằng cách sử dụng tham chiếu đến người dùng đầu tiên. Lưu ý rằng tôi chỉ chuyển một id duy nhất, không phải một mảng:
User.create({name:'test3',email:'[email protected]', friends:1}).exec(console.log);
Bây giờ, kết quả là cái này. Lưu ý rằng "bạn bè" không xuất hiện. Điều này là do thiết kế.
{ name: 'test3',
email: '[email protected]',
createdAt: '2016-12-01T22:07:34.988Z',
updatedAt: '2016-12-01T22:07:34.994Z',
id: 3 }
Hãy làm một phát với populate
để xem tình trạng hiện tại:
User.find().populate('friends').exec(console.log);
Bây giờ chúng ta thấy rằng người sử dụng thứ ba có bạn bè. Những người khác có một mảng trống.
[ { 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 } ]
Hãy tạo một thứ tư, lần này với hai người bạn:
User.create({name:'test4',email:'[email protected]', friends:[1,2]}).exec(console.log);
Hệ quả là (một lần nữa, không có tài sản bạn bè):
{ name: 'test4',
email: '[email protected]',
createdAt: '2016-12-01T22:07:50.539Z',
updatedAt: '2016-12-01T22:07:50.542Z',
id: 4 }
thời gian này, chúng tôi đã thông qua một mảng của id đến friends
. Hãy xem trạng thái hiện tại:
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 },
{ friends:
[ { name: 'test',
email: '[email protected]',
createdAt: '2016-12-01T22:06:19.723Z',
updatedAt: '2016-12-01T22:06:19.723Z',
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 } ]
Bây giờ, bạn làm cách nào để thêm bạn bè cho người dùng hiện tại? Thật lạ lùng: bạn phải đầu tiên find
người dùng, sau đó sử dụng chức năng add
trên mẫu kết quả, và sau đó, save
nó. Trong mã ngày của tôi, tôi có một hàm trợ giúp thực hiện điều đó. Vì vậy, đây là ví dụ:
User.findOne(1).populate('friends').exec(function(err,u){ u.friends.add(3);u.save(function(err){ if(err) console.error(err);});});
Bây giờ trong bảng điều khiển, chúng tôi không thấy kết quả. Hãy kiểm tra nội dung Người dùng ngay bây giờ:
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 } ]
Với phương pháp này, bạn thậm chí có thể thêm cùng một người dùng vào bộ sưu tập bạn bè!
User.findOne(1).populate('friends').exec(function(err,u){ u.friends.add(1);u.save(function(err){ if(err) console.error(err);});});
Hãy vui vẻ!
Bạn đã bao giờ tìm được giải pháp cho điều này chưa? Tôi hiện đang cố gắng làm chính xác điều tương tự .. – Michael
Xin chào Michael, đây là vấn đề, vì đó là cùng một mô hình, điều đó có nghĩa là cả hai đều có nhiều liên kết. Đường nước tự động tạo ra một bảng tham gia, nhưng nó messes lên sự thống trị. Vì vậy, bạn không thể sử dụng chức năng điền vào nó. Cách tốt nhất mà tôi đã tìm thấy là tạo một mô hình khác (ví dụ: Friends.js) và có một mô hình người dùng và sau đó là một tập hợp các mô hình người dùng khác. Sau đó, tôi thiết lập một cái móc 'populates' mà khi tôi cần nó. – scott
Cảm ơn scott! Đó chính xác là nơi tôi đang hướng tới, nhưng vui mừng khi biết nó thực sự hiệu quả với bạn. Tôi sẽ tiếp tục đi xuống con đường này. – Michael