如何在netjs sequize和mysql中添加多对多关联



很简单。我有博客模型、事件模型和标签模型。我想链接的博客标签多对多的关系。我能够在数据库加上博客标签和事件标签映射表中设置3个表。但是我找不到一种方法来插入标签的博客(我可以创建博客,但不能添加一个条目到blogtag表)。下面是代码。我可以创建博客,但不能给它们附加标签

import {
Table,
Column,
Model,
DataType,
CreatedAt,
UpdatedAt,
DeletedAt,
ForeignKey,
BelongsTo,
BelongsToMany
} from 'sequelize-typescript';
import { User } from '../../users/entities/user.entity';
import  {BlogTag, Tag} from '../../tags/entities/tag.entity'
@Table
export class Blog extends Model<Blog> {
@Column({
type: DataType.BIGINT,
allowNull: false,
autoIncrement: true,
unique: true,
primaryKey: true,
})
public id: number;
@Column({
type: DataType.STRING,
allowNull: false,
})
title: string;
@Column({
type: DataType.STRING,
allowNull: false,
})
blogData: string;
@ForeignKey(() => User)
@Column({
type: DataType.BIGINT,
allowNull: false,
})
userId: number;
@BelongsTo(() => User)
user: User;
@Column({
type: DataType.STRING,
allowNull: true,
})
imageUrl: string;
@Column({
type: DataType.BOOLEAN,
allowNull: false,
defaultValue: false,
})
checked: boolean;
@CreatedAt public createdAt: Date;
@UpdatedAt public updatedAt: Date;
@DeletedAt public deletedAt: Date;
@BelongsToMany( ()=> Tag, ()=> BlogTag)
tags: Tag[]
}

这是blogs.ts

import {
Table,
Column,
Model,
DataType,
CreatedAt,
UpdatedAt,
DeletedAt,
ForeignKey,
BelongsTo,
BelongsToMany,
} from 'sequelize-typescript';
import { User } from '../../users/entities/user.entity';
import { Event } from 'src/events/entities/event.entity';
import { Blog } from 'src/blogs/entities/blog.entity';

@Table
export class Tag extends Model<Tag> {
@Column({
type: DataType.BIGINT,
allowNull: false,
autoIncrement: true,
unique: true,
primaryKey: true,
})
public id: number;

@Column({
type: DataType.STRING,
allowNull: false,
})
name: string;
@BelongsToMany( ()=> Blog, ()=> BlogTag)
blogs: Blog[]

@BelongsToMany( ()=> Event, ()=> EventTag)
events: Event[]
}

@Table
export class BlogTag extends Model<BlogTag>{
@ForeignKey(() => Blog)
@Column({
type: DataType.BIGINT,
allowNull: false,
})
blogId: number;
@ForeignKey(() => Tag)
@Column({
type: DataType.BIGINT,
allowNull: false,
})
tagId: number;
}

这是标签这是我的博客地址

export class CreateBlogDto {
readonly title: string;
readonly checked: boolean;
readonly blogData: string;
imageUrl: string;
readonly userId: number;
tags: number[];
}

我的服务

async createBlog(createBlogDto: CreateBlogDto, blog_image) {
createBlogDto.imageUrl = blog_image.path;
// const tags = await this.tagService.getTagsByIds(createBlogDto.tags)
console.log(createBlogDto)
// console.log(tags)
// createBlogDto.tags = tags
return await this.blogRepository.create<Blog>(createBlogDto);

}这是我的控制器。

@UseGuards(AuthGuard('jwt'))
@Post()
create(
@Body() createBlogDto: CreateBlogDto,
@U[![**enter image description here**][1]][1]ploadedFile()
blog_image: Express.Multer.File,
) {
return this.blogsService.createBlog(createBlogDto, blog_image);

根据这个:这里在你定义你的模型分配标签给博客

你必须将你的BlogTag模型注入到你的服务中并调用BlogTag模型上的create方法使用创建的博客id和createBlogDto

中提供的每个标签
export class BlogService {
constructor(@InjectModel(BlogTag) private blogTagModel: typeof BlogTag,
@InjectModel(Blog) private blogModel: typeof Blog) {
}
async createBlog(createBlogDto: CreateBlogDto): Promise<Blog> {
const blog = await this.blogModel.create(createBlogDto);
const blogTags = await this.blogTagModel.findAll({
where: {
id: {[Op.in] : createBlogDto.tags}
},
});
if (createBlogDto.tags && createBlogDto.tags.length) {
await this.assignTagsToBlog(blog.id, createBlogDto.tags);
}
return blog.save();
}
async assignTagsToBlog(blogId: number, tags: number[]): Promise<Blog> {
const blog = await this.blogModel.findByPk(blogId);
for (const tag of tags) {
await  this.blogTagModel.create({
blogId: blogId,
tagId: tag
})
}
return blog;
}
}

assignTagsToBlog是一个函数,用于为blog添加和分配与参数

相同id的标签。

相关内容

  • 没有找到相关文章

最新更新