我正在使用Symfony / Doctrine 2创建一个简单的博客,并遇到php app/console doctrine:migrations:diff
命令的问题。
的信息:
我尝试过带有和不带有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;
}
答案 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