Symfony / Doctrine 2 で簡単なブログを作成していて、php app/console doctrine:migrations:diff
コマンドで問題が発生しました。
情報:
- Postgresql
- 関連するテーブル: Post、Category、fos_user
- 投稿テーブルには、user_id と category_id の 2 つの外部キーが必要です。
- diff は post テーブルに 1 つの外部キーのみを生成します
- ユーザーの manyToOne 宣言が投稿エンティティの doctrine 設定ファイルのカテゴリ ID の前にある場合、投稿テーブルは user_id 外部キーを取得しますが、category_id は取得しません (diff の実行時)。
- カテゴリ エンティティの manyToOne 宣言をユーザー エンティティの上に移動すると、投稿テーブルはカテゴリ ID を取得しますが、ユーザー 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;
}