2015-02-10 16 views
1

我有以下型號:帆多個型號副

Comment.js

module.exports = { 
    attributes: { 
     id : { 
      type : 'integer', 
      primaryKey : true 
     }, 
     objectId : { 
      type : 'string', 
      required : true 
     }, 
     comment : { 
      type : 'string', 
      required : true 
     } 
    } 

}; 

Image.js和的Video.js是相同的:

module.exports = { 
    attributes: { 
     id : { 
      type : 'integer', 
      primaryKey : true 
     }, 
     name : { 
      type: 'string' 
     }, 
     comments : { 
      collection : 'comment', 
      via : 'objectId' 
     } 

    } 

}; 

當我嘗試填充評論視頻或圖像模型陣列始終爲空(某些註釋已插入兩者)。

Image.find({id: 1}).populate('comments').exec(function(err, image) { 
    console.log(image); 
}); 

或者這...

Video.find({id: 1}).populate('comments').exec(function(err, video) { 
    console.log(video); 
}); 

我想獨立的視頻和圖像模型,並徵求意見,我想使用組合後的表。

TNX

+0

你使用了哪個數據庫? – sgress454 2015-02-11 01:14:56

+0

MySQL當然是 – Vlatko 2015-02-11 08:36:29

回答

0

你可以試試這個:

Image.js

module.exports = { 
    attributes: { 
     id : { 
      type : 'integer', 
      primaryKey : true 
     }, 
     name : { 
      type: 'string' 
     }, 
     comments : { 
      collection : 'comment', 
      via : 'image' 
     } 

    } 

}; 

Comment.js

module.exports = { 
    attributes: { 
     id : { 
      type : 'integer', 
      primaryKey : true 
     }, 
     image : { 
      model: 'image' 
     }, 
     comment : { 
      type : 'string', 
      required : true 
     } 
    } 

}; 

Quering

sails> Image.create({id:1, name: 'Image'}).then(console.log) 

sails> Comment.create({id:1, comment: 'Comment', image: 1}).then(console.log) 

sails>Image.find().populate('comments').then(console.log) 

sails> [ { comments: 
    [ { id: 1, 
     comment: 'Comment', 
     image: 1, 
     createdAt: Wed Feb 11 2015 15:13:50 GMT-0430 (VET), 
     updatedAt: Wed Feb 11 2015 15:13:50 GMT-0430 (VET) } ], 
    id: 1, 
    name: 'Image', 
    createdAt: Wed Feb 11 2015 15:13:01 GMT-0430 (VET), 
    updatedAt: Wed Feb 11 2015 15:13:01 GMT-0430 (VET) } ] 
+0

你有一些連接表嗎?你如何插入評論數據? – Vlatko 2015-02-10 19:34:50

+0

您使用哪個sails.js和waterline版本? – Vlatko 2015-02-11 13:52:59

+0

風帆-v 0.10.5 – pedronalbert 2015-02-11 19:04:40