Doctrine 2 Migrations Diff不会为每个表生成多个外键

时间:2015-08-18 03:26:58

标签: php postgresql symfony doctrine-orm doctrine-migrations

我正在使用Symfony / Doctrine 2创建一个简单的博客,并遇到php app/console doctrine:migrations:diff命令的问题。

的信息:

  • PostgreSQL的
  • 相关表格:帖子,类别,fos_user
  • 帖子表应该有2个外键:user_id和category_id
  • Diff仅在帖子表中生成一个外键
  • 如果用户的manyToOne声明出现在post实体的doctrine配置文件中的类别id之前,则post表获取user_id外键但没有category_id(当diff运行时)
  • 如果将类别实体的manyToOne声明移到用户实体的声明之上,则post表会获得category_id但不会收到user_id。

我尝试过带有和不带有inveredBy部分,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;
}

2 个答案:

答案 0 :(得分:0)

类别实体配置:

var myString = "Berlin, Paris, New York, San Francisco"
var myArray = myString.componentsSeparatedByString(",")
//Returns an array with the following values:  ["Berlin", " Paris", " New York", " San Francisco"]

应该是:

oneToMany:
        posts:
            targetEntity: Post

同样在post entity config中:

oneToMany:
        posts:
            targetEntity: Conduct\BlogBundle\Entity\Post

应该是:

manyToOne:
    category:
            targetEntity: Category

你应该在你没有提供完整命名空间的其他实体关系中做类似的事(评论和帖子)

答案 1 :(得分:0)

最后!发现了问题。

如果您有多个多对一声明,则应将它们组合在一起:

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