Skip to content

Commit

Permalink
* fix the inclusion loop problem among models.
Browse files Browse the repository at this point in the history
  • Loading branch information
redblaze committed Sep 25, 2013
1 parent bb64aa5 commit f2166f6
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,12 @@ module.exports = function() {
var me = this;

var cfg = this._table._lookupLinksToMap(name);

if (cfg) {
var otherTable = cfg.table.Table;
return cps.seq([
function(_, cb) {
cfg.table.findById(conn, me.get(cfg.key), cb);
otherTable.findById(conn, me.get(cfg.key), cb);
},
function(res, cb) {
me._data[cfg.name] = res;
Expand All @@ -150,10 +152,12 @@ module.exports = function() {
var me = this;

var cfg = this._table._lookupLinkedByMap(name);

if (cfg) {
var otherTable = cfg.table.Table;
return cps.seq([
function(_, cb) {
cfg.table.find(conn, cfg.table.baseQuery('where ' + cfg.key + ' = ?', [me.getId()]), cb);
otherTable.find(conn, cfg.table.baseQuery('where ' + cfg.key + ' = ?', [me.getId()]), cb);
},
function(res, cb) {
me._data[cfg.name] = res;
Expand All @@ -170,16 +174,19 @@ module.exports = function() {

var cfg = this._table._lookupRelatesToMap(name);
if (cfg) {
var otherTable = cfg.table.Table;
var throughTable = cfg.through.Table;

return cps.seq([
function(_, cb) {
var _q = 'select t.* from ' +
cfg.table.getName() + ' t, ' +
cfg.through.getName() + ' r where ' +
otherTable.getName() + ' t, ' +
throughTable.getName() + ' r where ' +
'r.' + cfg['leftKey'] + ' = ? and ' +
'r.' + cfg['rightKey'] + ' = t.' + cfg.table.getIdFieldName()
'r.' + cfg['rightKey'] + ' = t.' + otherTable.getIdFieldName()
;
var q = DB.format(_q, [me.getId()]);
cfg.table.find(conn, q, cb);
otherTable.find(conn, q, cb);
},
function(res, cb) {
me._data[cfg.name] = res;
Expand Down

0 comments on commit f2166f6

Please sign in to comment.