我正在使用 Symfony / Doctrine 2 创建一个简单的博客,并遇到了php app/console doctrine:migrations:diff
命令问题。
信息:
- PostgreSQL
- 相关表:Post、Category、fos_user
- Post 表应该有 2 个外键:user_id 和 category_id
- Diff 只在 post 表中生成一个外键
- 如果用户的 manyToOne 声明出现在 post 实体的教义配置文件中的类别 id 之前,则 post 表将获得 user_id 外键但没有 category_id(当差异运行时)
- 如果将类别实体的 manyToOne 声明移到用户实体之上,则 post 表将获得 category_id 但没有 user_id。
我已经尝试了带有和不带有 inversedBy 部分、joinColumn 部分等的 manyToOne 声明。下面是我的 YML 配置。使用此配置,将在 post 表中创建一个 user_id 外键,但没有类别 ID。(见帖子配置的底部)
如果有人有任何想法,我将不胜感激!
发布实体配置
Conduct\BlogBundle\Entity\Post:
type: entity
table: null
manyToOne:
user:
targetEntity: Acme\UserBundle\Entity\User
inversedBy: posts
joinColumn:
name: user_id
referencedColumnName: id
manyToOne:
category:
targetEntity: Conduct\BlogBundle\Entity\Category
inversedBy: posts
joinColumn:
name: category_id
referencedColumnName: id
lifecycleCallbacks: { }
类别实体配置
Conduct\BlogBundle\Entity\Category:
type: entity
table: null
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
title:
type: string
length: 255
oneToMany:
posts:
targetEntity: Conduct\BlogBundle\Entity\Post
mappedBy: category
lifecycleCallbacks: { }
用户实体配置
Acme\UserBundle\Entity\User:
type: entity
table: fos_user
id:
id:
type: integer
generator:
strategy: AUTO
oneToMany:
posts:
targetEntity: Conduct\BlogBundle\Entity\Post
mappedBy: user
类别实体代码
class Category
{
private $posts;
public function __construct()
{
$this->posts = new ArrayCollection();
}
}
发布实体代码
class Post
{
protected $category;
}