2015-04-02 63 views
2

我从sails.js开始,我完全失去了我的SQL查询。Sails.js协会

我有以下表格:

genres 
+-----------+--------------+------+-----+ 
| Field  | Type   | Null | Key | 
+-----------+--------------+------+-----+ 
| id  | int(6)  | NO | PRI | 
| name  | varchar(100) | NO |  | 
| slug  | varchar(255) | NO |  | 
| type  | varchar(32) | NO |  | 
| parent_id | int(11)  | YES | MUL | 
+-----------+--------------+------+-----+ 
genres_radios 
+----------+--------+------+-----+ 
| Field | Type | Null | Key | 
+----------+--------+------+-----+ 
| genre_id | int(6) | NO | MUL | 
| radio_id | int(6) | NO | MUL | 
+----------+--------+------+-----+ 
radios 
+-----------+--------------+------+-----+ 
| Field  | Type   | Null | Key | 
+-----------+--------------+------+-----+ 
| id  | int(5)  | NO | PRI | 
| name  | varchar(100) | NO |  | 
| slug  | varchar(100) | NO |  | 
| url  | varchar(100) | NO |  | 
+-----------+--------------+------+-----+ 

我要检索的无线电设备及其相关流派。我设法使用Model.query(“Select * FROM ...”)来做到这一点,但我想用填充方法来做到这一点。我有一个看的文档,但我有点困惑与“内经”,“通过”,...

回答

0

这应做到:

// api/models/Genres.js 
module.exports = { 
    attributes : { 
     name : { 
      type: 'string' 
     }, 
     slug : { 
      type: 'string' 
     }, 
     type : { 
      type: 'string' 
     }, 
     radios : { 
      collection: 'Radios', 
      through: 'genres_radios' 
     } 
    } 
} 

// api/models/Radios.js 
module.exports = { 
    attributes : { 
     name : { 
      type: 'string' 
     }, 
     slug : { 
      type: 'string' 
     }, 
     url : { 
      type: 'string' 
     }, 
     genres : { 
      collection: 'genre', 
      through: 'genres_radios' 
     } 
    } 
} 

// api/models/Genres_radios.js 
module.exports = { 
    attributes = { 
     'Genre_id': { 
      columnName:'genre_id', 
      type:'integer', 
      foreignKey:'true', 
      references:'genres', 
      on:'id', 
      via:'genres' 
     }, 
     'Radio_id': { 
      columnName:'radio_id', 
      type:'integer', 
      foreignKey:'true', 
      references:'radios', 
      on:'id', 
      via:'radios' 
     } 
    } 
} 

然后你就可以提出以下要求:

Radio.findOne({name:"RadioName"}).populate("genres").then(function(radio){ 
    console.log(radio); 
}) 
2

那么如果您遵循了Sails.js Model documentationmany-many association docs你的模型应该是这个样子:

// api/models/genre.js 
module.exports = { 
    attributes : { 
     name : { 
      type: 'string' 
     }, 
     slug : { 
      type: 'string' 
     }, 
     type : { 
      type: 'string' 
     }, 
     radios : { 
      collection: 'radio', 
      via: 'genres' 
     } 

    } 
} 

// api/models/radio.js 
module.exports = { 
    attributes : { 
     name : { 
      type: 'string' 
     }, 
     slug : { 
      type: 'string' 
     }, 
     url : { 
      type: 'string' 
     }, 
     genres : { 
      collection: 'genre', 
      via: 'radios' 
     } 

    } 
} 

水线会在内部为您创建许多查询表。所有你需要得到你的收音机的流派是填充“流派”属性。

Radio.findOne({name:"RadioName"}).populate("genres").then(function(radio){ 
    console.log(radio); //radio.genres will have all the genres associated with this radio. 
}) 

我真的推荐看看many-many association docs。他们正是你所需要的。

+0

我已经做了这一点,但我的结果是,流派属性是空的。看看你写的是什么,当我没有告诉任何genre_id它应该查找的地方(radio_id genre_id存储在genres_radios表中)时,我不明白我能够获取收音机的类型 – johnmalkovitch 2015-04-03 08:38:42

+0

啊,它有点奇怪,因为你已经有一个现有的表。实际上,Sails会为您创建genres_radios查找表。 – 2015-04-07 06:36:29