簡體   English   中英

教義2遷移差異不會產生每個表超過一個外鍵

[英]Doctrine 2 Migrations Diff Won't Generate More Than One Foreign Key per Table

我正在用Symfony / Doctrine 2創建一個簡單的博客,並且遇到了php app/console doctrine:migrations:diff命令的問題。

信息:

  • PostgreSQL
  • 相關表:Post,Category,fos_user
  • 帖子表應具有2個外鍵:user_id和category_id
  • Diff僅在發布表中生成一個外鍵
  • 如果用戶的manyToOne聲明出現在發布實體的學說配置文件中的類別ID之前,則發布表將獲得一個user_id外鍵,但沒有category_id(當差異運行時)
  • 如果將類別實體的manyToOne聲明移到用戶實體的聲明之上,則發布表將獲得category_id,但不會獲得user_id。

我嘗試了具有和不具有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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM