2016-08-05 1 views
1

A(1) - > B(many) - > C(many) - > D(many)のようなデータモデルをお持ちで、 Dのオブジェクトは、しかし、あなたはAの基準を持って、どのようにそれを行うことができますか?ここ条件を指定してmulitipleテーブルに結合する本棚クエリ

詳細は:

const Contact = bookshelf.Model.extend({ 
    sites: function() { 
    return this.hasMany(Site); 
    }, 
}); 

const Contacts = bookshelf.Collection.extend({ 
    model: Contact, 
}); 

const Site = bookshelf.Model.extend({ 
    siteAttendances: function() { 
    return this.hasMany(SiteAttendance); 
    }, 
    siteSupervisor: function() { 
    return this.belongsTo(Contact); 
    }, 
}); 

const Sites = bookshelf.Collection.extend({ 
    model: Site, 
}); 

const SiteAttendance = bookshelf.Model.extend({ 
    site: function() { 
    return this.belongsTo(Site); 
    }, 
    incidents: function() { 
    return this.hasMany(Incident); 
    }, 
}); 

const SiteAttendances = bookshelf.Collection.extend({ 
    model: SiteAttendance, 
}); 

const Incident = bookshelf.Model.extend({ 
    siteAttendance: function() { 
    return this.belongsTo(SiteAttendance); 
    } 
}); 

const Incidents = bookshelf.Collection.extend({ 
    model: Incident, 
}); 

私は連絡先のID(オブジェクト)を持っているが、私は、インシデント(Dオブジェクト)のオブジェクトをしたいと私は唯一の本棚でこれを行うことができ、私は思ったんだけど。 js?物事を複雑にするために、すべての連絡先について、多くのインシデントだけがサイトとSiteAttendanceがあります。 1つの連絡先IDで多くのサイトが生成されるため、throughを使用してインシデントからサイトに送信する方法があるかもしれませんが、それを動作させることはできませんでした。私は連絡先から始めてwithRelatedをやっていないと正しいアプローチです(サイトやSiteAttendanceの数が多いので)が、間違っている可能性があります。

答えて

0

深い関係のために間違ってthroughを使用することはできません。 withRelatedオプションは、連絡先のincidentsを取得する3つのクエリを作成します。 気にしない場合は、withRelated: "sites.siteAttendances.incidents"を保存します。

+0

これは私が考えたものです。このようなことについては、ブックシェルフにぶつかってknex vsに脱落することについてコンセンサスがありますか? – ricka

0

私はまったく同じ問題を抱えていて、それを行うための "Bookshelf-y"の方法があるかどうかも疑問に思っていました。私はいくつかのknexベースのコードを実装しています。つまり、指定された結合で単一のSELECT文を出力します。

は約そうのようなあなたの例に変換します。

const exampleVal = Incident.forge(); 
    const toSiteAt = exampleVal.siteAttendance(); 
    const incTable = toSiteAt.relatedData.parentTableName; 
    const saTable = toSiteAt.relatedData.targetTableName; 
    const saKey = toSiteAt.relatedData.targetIdAttribute; 
    const forKeySA = toSiteAt.relatedData.foreignKey; 

    const toSite  = toSiteAt.site(); 
    const siteTable = toSite.relatedData.targetTableName; 
    const siteKey = toSite.relatedData.targetIdAttribute; 
    const forKeyS = toSite.relatedData.foreignKey; 

    const toContact = toSite.siteSupervisor(); 
    const contctTable = toContact.relatedData.targetTableName; 
    const contctKey = toContact.relatedData.targetIdAttribute; 
    const forKeyC  = toContact.relatedData.foreignKey; 

    return Incident.query(qb => { 
    qb.innerJoin(saTable,  `${incTable}.${forKeySA}`, `${saTable}.${saKey}`) 
     .innerJoin(siteTable, `${saTable}.${forKeyS}`, `${siteTable}.${siteKey}`) 
     .innerJoin(contctTable, `${siteTable}.${forKeyC}`, `${contctTable}.${contctKey}`) 
     .whereIn(`${contctTable}.id`, myContactId); // <1> 
    }).fetchAll(); 

< 1>ここでは、あなたの連絡先のIDは皆に

質問です:に実際にあるのいくつかのより多くの "本棚-Y" の方法これを解決する?

+0

特に、これは必要な情報を取得するための1つのクエリなので、この点についてもう1つの取り組みを非常に感謝します。おそらく誰もこの回答を見ていないので、あなたのソリューションで新しい質問を作成し、最良の "Bookshelf-y"の方法を尋ねる価値があります。 – ricka

関連する問題