首页 > 解决方案 > TypeORM:如何将现有类别与新帖子链接

问题描述

我正在构建一个基于 Wordpress 数据库模式的博客休息 API!

我使用 typeORM 框架将wp_posts表与wp_term_taxonomy具有多对多关系 的表链接起来

现在的问题是,当我使用级联功能创建包含类别的帖子时

即使它已经存在,它也会生成新的类别我不能在新帖子中使用现有的类别!

所以我只需要将现有类别(分类法)与新帖子联系起来。

编码:

private createPost = async (request: express.Request, response: express.Response) => {

    const data: WpPostsModel = request.body;
    const entries: any[] = request.body.taxonomies;
    let taxonomies = [];

    for (let i = 0; i < entries.length; i++) {
        const entry = entries[i];

        let newTaxonomy = new WpTermTaxonomyModel();

        newTaxonomy = {
            taxonomy: entry.taxonomy,
            description: '',
            term: {
                name: entry.term.name,
                slug: entry.term.name.replace(/\s+/g, '-'),
                term_group: 0,
                // term_id: entry.term.term_id
            }
        }

        taxonomies.push(newTaxonomy);
    }


    const newPost = await this.postRepository.create({
        post_title: data.post_title,
        post_content: data.post_content,
        post_excerpt: data.post_excerpt,
        post_type: 'post',
        post_mime_type: '',
        comment_status: 'closed',
        ping_status: 'closed',
        to_ping: '',
        pinged: '',
        comment_count: 0,
        post_parent: 0,
        post_status: 'publish',
        post_content_filtered: '',
        post_password: '',
        post_name: ( data.post_title ) ? data.post_title.replace(/\s+/g, '-') : '',
        post_date: new Date(),
        post_date_gmt: new Date(),
        post_modified: new Date(),
        post_modified_gmt: new Date(),
        taxonomies
    });

    await this.postRepository.save(newPost);

    response.send(newPost);

}

wp_posts 实体

@Entity({ name: 'wp_posts', synchronize: false })
export class WpPostsModel {

    @PrimaryGeneratedColumn()
    public ID?: number;

    @Column()
    public post_date?: string;

    @Column()
    public post_date_gmt?: string;

    @Column()
    public post_content?: string;

    @Column()
    public post_title?: string;

    ...etc

    @ManyToMany(() => WpTermTaxonomyModel)
    @JoinTable({
        name: 'wp_term_relationships',
        joinColumn: {
            name: 'object_id',
            referencedColumnName: 'ID'
        },
        inverseJoinColumn: {
            name: 'term_taxonomy_id',
            referencedColumnName: 'termTaxonomyId'
        }
    })
    public categories?: WpTermTaxonomyModel[];

}

wp_term_taxonomy 实体

@Entity({ name: 'wp_term_taxonomy', synchronize: false })
export class WpTermTaxonomyModel {

    @PrimaryGeneratedColumn({ name: 'term_taxonomy_id' })
    public termTaxonomyId?: number;

    @OneToOne(() => WpTermsModel)
    @JoinColumn({ name: 'term_id' })
    public term?: WpTermsModel;

    @Column()
    public taxonomy?: string;

    @Column()
    public description?: string;

    @Column()
    public parent?: number;

}

wp_terms 实体

@Entity({ name: 'wp_terms', synchronize: false })
export class WpTermsModel {

    @PrimaryGeneratedColumn()
    public term_id?: number;

    @Column()
    public name?: string;

    @Column()
    public slug?: string;

    @Column()
    public term_group?: number;
}

标签: javascriptmysqlnode.jsrelational-databasetypeorm

解决方案


推荐阅读