0

我正在使用 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;
}
4

2 回答 2

0

类别实体配置:

oneToMany:
        posts:
            targetEntity: Post

应该:

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

同样在后期实体配置中:

manyToOne:
    category:
            targetEntity: Category

应该:

manyToOne:
    category:
            targetEntity: Conduct\BlogBundle\Entity\Category

您应该在未提供完整命名空间(评论和 postmeta)的其他实体关系中执行类似操作

于 2015-08-18T05:24:52.570 回答
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
于 2015-08-24T01:52:11.433 回答