gpt4 book ai didi

javascript - 如何在typeORM中的@ManyToMany中保存关系

转载 作者:行者123 更新时间:2023-11-30 11:08:02 24 4
gpt4 key购买 nike

有 2 个实体名为 ArticleClassification。它们之间的关系是@ManyToMany

这是我的问题:如何保存关系?

我的代码如下:

  @Entity()
export class Article {
@PrimaryGeneratedColumn()
id: number;

@Column()
name: string;

@CreateDateColumn()
createTime: Date;

@UpdateDateColumn()
updateTime: Date;

@Column({
type: 'text',
})
content: string;

@Column({
default: 0,
})
likeAmount: number;

@Column({
default: 0,
})
commentAmount: number;
}

@Entity()
export class Classification {
@PrimaryGeneratedColumn()
id: number;

@CreateDateColumn()
createTime: Date;

@UpdateDateColumn()
updateTime: Date;

@Column()
name: string;

@ManyToMany(type => Article)
@JoinTable()
articles: Article[];
}

我可以成功保存文章分类。但我不确定如何保存它们之间的关系。

我试图通过以下代码保存关系:

async create(dto: ArticleClassificationDto): Promise<any> {
const article = this.repository.save(dto);
article.then(value => {
console.log(value);//console the object article
value.classification.forEach(item => {
const classification = new Classification();
classification.id = item.id;
classification.articles = [];
classification.articles.push(value);
this.classificationService.save(classification);
})
});
console.log(article);
return null;
}

还有这样的帖子数据结构

    {
"name":"artile name",
"content":"article content",
"classification":[{
"id":4
},{
"id":3
}]
}

一开始,它是有效的。

enter image description here

但是当我再次发布数据时,旧记录被替换而不是创建另一条记录。

enter image description here

接下来我该做什么?

请看下面的代码。

async create(dto: ArticleClassificationDto): Promise<any> {
this.repository.save(dto).then(article => {
article.classification.forEach(item => {
this.ClassificationRepository.findOne(
{
// the privous method is get all the articles from databse and push into this array
// relations: ['articles'],
where: { id: item }// now I change the data strcture, just contains id instead of {id}
}
).then(classification => {
// console.log(article);
console.log(classification);
// cmd will show ' UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'push' of undefined' withous below line code. But if I init the array manually,the old record will be replaced again.
// classification.articles = [];
classification.articles.push(article);
this.ClassificationRepository.save(classification);
});
})
})
return null;
}

最佳答案

如何保存关系?

假设您有一组文章,并且您想要创建与分类实体的关系。您只需将数组分配给属性 articles 并保存实体; typeorm 将自动创建关系。

classification.articles = [article1, article2];
await this.classificationRepository.save(classification);

为此,文章实体必须已经保存。如果想让typeorm自动保存文章实体,可以设置cascadetrue

@ManyToMany(type => Article, article => article.classifications, { cascade: true })

你的例子

async create(dto: ArticleClassificationDto): Promise<any> {
let article = await this.repository.create(dto);
article = await this.repository.save(article);
const classifications = await this.classificationRepository.findByIds(article.classification, {relations: ['articles']});
for (const classification of classifications) {
classification.articles.push(article);
}
return this.classificationRepository.save(classifications);
}

关于javascript - 如何在typeORM中的@ManyToMany中保存关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54885935/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com