Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions packages/pinia-orm/src/model/attributes/relations/HasMany.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Collection, Element } from '../../../data/Data'
import type { Query } from '../../../query/Query'
import type { Model, PrimaryKey } from '../../Model'
import type { Dictionary } from './Relation'
import { isArray } from '../../../support/Utils'
import { Relation } from './Relation'

export class HasMany extends Relation {
Expand Down Expand Up @@ -76,7 +77,11 @@ export class HasMany extends Relation {
const dictionary = this.buildDictionary(query.get(false))

models.forEach((model) => {
const key = model[this.getKey(this.localKey)]
const key = this.getKey(
isArray(this.localKey)
? this.localKey.map(key => model[key])
: model[this.localKey]
)

dictionary[key]
? model.$setRelation(relation, dictionary[key])
Expand All @@ -89,8 +94,12 @@ export class HasMany extends Relation {
*/
protected buildDictionary (results: Collection): Dictionary {
return this.mapToDictionary(results, (result) => {
const key = this.getKey(this.foreignKey)
return [result[key], result]
const key = this.getKey(
isArray(this.foreignKey)
? this.foreignKey.map(key => result[key])
: result[this.foreignKey]
)
return [key, result]
})
}

Expand Down