mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
fixed bug with empty objects when multiple joins are used
This commit is contained in:
parent
78deb851eb
commit
887cedaeb3
46
sample/sample23-nested-joins/app.ts
Normal file
46
sample/sample23-nested-joins/app.ts
Normal file
@ -0,0 +1,46 @@
|
||||
import {createConnection, CreateConnectionOptions} from "../../src/typeorm";
|
||||
import {Post} from "./entity/Post";
|
||||
import {Author} from "./entity/Author";
|
||||
import {Category} from "./entity/Category";
|
||||
|
||||
const options: CreateConnectionOptions = {
|
||||
driver: "mysql",
|
||||
connection: {
|
||||
host: "192.168.99.100",
|
||||
port: 3306,
|
||||
username: "root",
|
||||
password: "admin",
|
||||
database: "test",
|
||||
autoSchemaCreate: true,
|
||||
logging: {
|
||||
logOnlyFailedQueries: true,
|
||||
logFailedQueryError: true
|
||||
}
|
||||
},
|
||||
entities: [Post, Author, Category]
|
||||
};
|
||||
|
||||
createConnection(options).then(connection => {
|
||||
|
||||
let postRepository = connection.getRepository(Post);
|
||||
|
||||
let post = new Post();
|
||||
post.text = "Hello how are you?";
|
||||
post.title = "hello";
|
||||
|
||||
postRepository
|
||||
.persist(post)
|
||||
.then(post => {
|
||||
return postRepository
|
||||
.createQueryBuilder("post")
|
||||
.leftJoin("post.categories", "categories")
|
||||
.leftJoin("categories.author", "author")
|
||||
.where("post.id=1")
|
||||
.getSingleResult();
|
||||
})
|
||||
.then(loadedPost => {
|
||||
console.log("loadedPosts: ", loadedPost);
|
||||
})
|
||||
.catch(error => console.log(error.stack));
|
||||
|
||||
}, error => console.log("Cannot connect: ", error));
|
||||
13
sample/sample23-nested-joins/entity/Author.ts
Normal file
13
sample/sample23-nested-joins/entity/Author.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import {PrimaryColumn, Column} from "../../../src/columns";
|
||||
import {Table} from "../../../src/tables";
|
||||
|
||||
@Table("sample23_author")
|
||||
export class Author {
|
||||
|
||||
@PrimaryColumn("int", { generated: true })
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
name: string;
|
||||
|
||||
}
|
||||
20
sample/sample23-nested-joins/entity/Category.ts
Normal file
20
sample/sample23-nested-joins/entity/Category.ts
Normal file
@ -0,0 +1,20 @@
|
||||
import {PrimaryColumn, Column} from "../../../src/columns";
|
||||
import {Table} from "../../../src/tables";
|
||||
import {Author} from "./Author";
|
||||
import {ManyToMany} from "../../../src/decorator/relations/ManyToMany";
|
||||
import {JoinTable} from "../../../src/decorator/relations/JoinTable";
|
||||
|
||||
@Table("sample23_category")
|
||||
export class Category {
|
||||
|
||||
@PrimaryColumn("int", { generated: true })
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
name: string;
|
||||
|
||||
@ManyToMany(type => Author)
|
||||
@JoinTable()
|
||||
author: Author;
|
||||
|
||||
}
|
||||
25
sample/sample23-nested-joins/entity/Post.ts
Normal file
25
sample/sample23-nested-joins/entity/Post.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import {PrimaryColumn, Column} from "../../../src/columns";
|
||||
import {Table} from "../../../src/tables";
|
||||
import {Category} from "./Category";
|
||||
import {ManyToMany} from "../../../src/decorator/relations/ManyToMany";
|
||||
import {JoinTable} from "../../../src/decorator/relations/JoinTable";
|
||||
|
||||
@Table("sample23_post")
|
||||
export class Post {
|
||||
|
||||
@PrimaryColumn("int", { generated: true })
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
title: string;
|
||||
|
||||
@Column()
|
||||
text: string;
|
||||
|
||||
@ManyToMany(type => Category, {
|
||||
cascadeAll: true
|
||||
})
|
||||
@JoinTable()
|
||||
categories: Category[];
|
||||
|
||||
}
|
||||
@ -45,7 +45,11 @@ export class RawSqlResultsToEntityTransformer {
|
||||
const groupedResults = OrmUtils.groupBy(rawSqlResults, result => alias.getPrimaryKeyValue(result, metadata.primaryColumn));
|
||||
return groupedResults
|
||||
.map(group => this.transformIntoSingleResult(group.items, alias, metadata))
|
||||
.filter(res => !!res);
|
||||
.filter(res => !!res)
|
||||
.map(res => {
|
||||
// console.log("res: ", res);
|
||||
return res;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@ -70,8 +74,9 @@ export class RawSqlResultsToEntityTransformer {
|
||||
const relationAlias = this.aliasMap.findAliasByParent(alias.name, relation.name);
|
||||
if (relationAlias) {
|
||||
const relatedEntities = this.groupAndTransform(rawSqlResults, relationAlias);
|
||||
const result = (relation.isManyToOne || relation.isOneToOne) ? relatedEntities[0] : relatedEntities;
|
||||
if (result) {
|
||||
const isResultArray = relation.isManyToMany || relation.isOneToMany;
|
||||
const result = !isResultArray ? relatedEntities[0] : relatedEntities;
|
||||
if (result && (!isResultArray || result.length > 0)) {
|
||||
if (relation.isLazy) {
|
||||
entity["__" + relation.propertyName + "__"] = result;
|
||||
} else {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user