javascript - 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;
}
解决方案
推荐阅读
- android - 如何在 webview 中打开相机?
- python - kmeans++的向量化实现
- maven - 无法确定 pom.xml 的全局设置
- oracle - Entity Framework db first:状态机器关联
- python-docx - 在 python-docx 中创建 word 字段以从 word 文档的第 2 页自动生成页脚(例如 # Page / # Total Pages)?
- excel - 用于匹配字符串并复制它的 VBA 代码
- typo3 - TYPO3:在 TCA 中使用 ###PAGE_TSCONFIG_ID### 和 addRecord
- machine-learning - spark 1.6 中用于高基数分类变量的散列编码器
- freertos - 低优先级任务需要先启动 FreeRTOS
- android - 视图的简单上滑动画