2015-04-02 71 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