我对 symfony 或教义没有太多经验,但我的处境很奇怪。
我有一个简单的用户和帖子实体。问题是用户可以在登录时发布内容。
问题是我想将我的表“post”和“user”与post.user_id和user.id链接起来
因此,根据学说,我决定在Post中建立一个简单的 manyToOne 关系:
(Post.orm.yml)
Whatever\PostBundle\Entity\Post:
type: entity
table: post
repositoryClass: Whatever\PostBundle\Repository\PostRepository
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
content:
type: text
userId:
type: integer
column: user_id
createdAt:
type: datetime
column: created_at
manyToOne:
user:
targetEntity: Whatever\UserBundle\Entity\User
lifecycleCallbacks: { }
生成的实体和学说:模式:更新完成
现在我可以通过帖子获取用户信息,但由于 user_id null 也无法再发布任何内容
使用参数 ["content of the post", null, "2017-04-21 11:27:04"] 执行 'INSERT INTO post (content, user_id, created_at) VALUES (?, ?, ?)' 时发生异常:
如您所见,user_id 为空(我尝试在控制器、实体构造或表单字段中手动设置它,结果相同)
还没完。因为如果我评论/删除 Post.orm.yml 的一部分:
manyToOne:
user:
targetEntity: Whatever\UserBundle\Entity\User
然后我可以再次发布......但我不明白为什么。(顺便说一句,我无法再通过帖子获取用户信息。所以我仍然需要它)
有人可以给我一个解释吗?我已经解决这个问题2天了。你是我最后的希望。