Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution for Fixing Binding Issues with Array in JSON Field in HasManyRelation #2667

Open
UnRealSKY opened this issue Jun 5, 2024 · 1 comment

Comments

@UnRealSKY
Copy link

UnRealSKY commented Jun 5, 2024

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
  }
  
  /** ... */
}
@kapouer
Copy link
Contributor

kapouer commented Aug 30, 2024

It would be great to make a PR that references this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants