You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When relation: Model.HasManyRelation is used and join.from is a JSON field with its content being an array, I observed that withGraphFetch using builder.toKnexQuery().toString() can actually query the correct rows. However, it fails to bind back to own. To address this, I modified the onAfter2 method in RelationFindOperation.js.
import _ from 'lodash'
class RelationFindOperation extends FindOperation {
/** ... */
getOwnKeys(ownerProp, own) {
const ownerKeysHandlers = [
{
match: (ownerProp, own) => ownerProp.props.length > 1,
handler: (ownerProp, own) => ownerProp.propKey(own)
},
{
match: (ownerProp, own) => _.isArray(own[ownerProp.props[0]]),
handler: (ownerProp, own) =>
_.map(own[ownerProp.props[0]], (ownProp) =>
ownerProp.propKey({ own, [ownerProp.props[0]]: ownProp })
)
},
{
match: () => true,
handler: (ownerProp, own) => ownerProp.propKey(own)
}
]
for (const { match, handler } of ownerKeysHandlers) {
const matched = match(ownerProp, own)
if (!matched) {
continue
}
return handler(ownerProp, own)
}
}
onAfter2(_, related) {
const isOneToOne = this.relation.isOneToOne()
if (this.assignResultToOwner && this.owner.isModels) {
const owners = this.owner.modelArray
const relatedByOwnerId = new Map()
for (let i = 0, l = related.length; i < l; ++i) {
const rel = related[i]
const key = this.relation.relatedProp.propKey(rel)
let arr = relatedByOwnerId.get(key)
if (!arr) {
arr = []
relatedByOwnerId.set(key, arr)
}
arr.push(rel)
}
}
for (let i = 0, l = owners.length; i < l; ++i) {
const own = owners[i]
const keys = this.getOwnKeys(this.relation.ownerProp, own)
for (const key of keys) {
const related = relatedByOwnerId.get(key)
if (isOneToOne) {
own[this.relationProperty] = (related && related[0]) || null
} else {
own[this.relationProperty] = [].concat(
own[this.relationProperty] || [],
related || []
)
}
}
}
return related
}
/** ... */
}
The text was updated successfully, but these errors were encountered:
When relation:
Model.HasManyRelation
is used andjoin.from
is a JSON field with its content being an array, I observed thatwithGraphFetch
usingbuilder.toKnexQuery().toString()
can actually query the correct rows. However, it fails to bind back toown
. To address this, I modified theonAfter2
method inRelationFindOperation.js
.The text was updated successfully, but these errors were encountered: