我正在用Symfony/Doctrine 2创建一个简单的博客,并且遇到了php app/console doctrine:migrations:diff
命令的问题。
信息:
- Postgresql
- 相关表:Post, Category, fos_user
- Post表应该有2个外键:user_id和category_id
- Diff在post表中只生成一个外键
- 如果用户的manyToOne声明出现在post实体的doctrine配置文件中的category id之前,post表将获得user_id外键,但没有category_id(当diff运行时)
- 如果您将类别实体的manyToOne声明移动到用户实体之上,则post表将获得category_id但没有user_id。
我已经尝试了许多toone声明有和没有inversedBy部分,joinColumn部分等。下面是我的YML配置。使用此配置,将在post表中创建user_id外键,但不创建类别id。(见post配置底部)
如果有人有任何想法,我将非常感激!
Post实体配置
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();
}
}
Post Entity Code
class Post
{
protected $category;
}
类别实体配置:
oneToMany:
posts:
targetEntity: Post
应:oneToMany:
posts:
targetEntity: Conduct'BlogBundle'Entity'Post
类似于post entity config:
manyToOne:
category:
targetEntity: Category
应:manyToOne:
category:
targetEntity: Conduct'BlogBundle'Entity'Category
您应该在没有提供完整名称空间的其他实体关系中执行类似操作(注释&postmeta)
终于!找到问题了。
当你有多个多对一声明时,它们应该像这样组合在一起:
manyToOne:
category:
targetEntity: Conduct'BlogBundle'Entity'Category
inversedBy: posts
joinColumn:
name: category_id
referencedColumnName: id
user:
targetEntity: Acme'UserBundle'Entity'User
inversedBy: posts
joinColumn:
name: user_id
referencedColumnName: id