![](/img/trans.png)
[英]Extra changeColumns in Doctrine generate-migrations-diff
[英]Doctrine 2 Migrations Diff Won't Generate More Than One Foreign Key per Table
我正在用Symfony / Doctrine 2創建一個簡單的博客,並且遇到了php app/console doctrine:migrations:diff
命令的問題。
信息:
我嘗試了具有和不具有inversedBy部分,joinColumn部分等的manyToOne聲明。以下是我的YML配置。 使用此配置,將在發布表中創建一個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;
}
類別實體配置:
oneToMany:
posts:
targetEntity: Post
應該:
oneToMany:
posts:
targetEntity: Conduct\BlogBundle\Entity\Post
同樣在發布實體配置中:
manyToOne:
category:
targetEntity: Category
應該:
manyToOne:
category:
targetEntity: Conduct\BlogBundle\Entity\Category
您應該在沒有提供完整名稱空間的其他實體關系中執行類似的操作(注釋和后修飾)
最后! 找到了問題。
當您有多個多對一聲明時,應將它們分組在一起,如下所示:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.