Doctrine 2 で単一のテーブル/クラスを使用して、複数のエンティティに適用できるサブスクリプション モデルを実現しようとしています。以下の例による説明を参照してください。
スキーマ (yml):
User:
type: entity
table: users
id: int
name: string
Subscription:
type: entity
table: subscriptions
id: int
object_type: string
object_id: int
user_id: int
Feature:
type: entity
table: features
id: int
name: string
manyToMany:
subscribers:
targetEntity: User
joinTable:
name: subscriptions
joinColumns:
object_id:
referencedColumnName: id
Issue:
type: entity
table: issues
id: int
subject: string
manyToMany:
subscribers:
targetEntity: User
joinTable:
name: subscriptions
joinColumns:
object_id:
referencedColumnName: id
テーブル データは次のようになります。
users:
| id | name |
| 1 | John |
| 2 | Joe |
features:
| id | name |
| 1 | Feature A |
| 2 | Feature B |
issues:
| id | subject |
| 1 | Issue 1 |
| 2 | Issue 2 |
subscriptions:
| id | object_type | object_id | user_id
| 1 | feature | 1 | 1 <- John is subscribed to Feature A
| 2 | issue | 1 | 1 <- John is subscribed to Issue 1
私が期待していたのは、モデルの manyToMany リレーションで使用できる追加の「区別」フィールドです。たとえば、次のようになります。
manyToMany:
subscribers:
targetEntity: User
joinTable:
name: subscriptions
joinColumns:
object_id:
referencedColumnName: id
object_type:
value: feature
後者のソウルションが教義に存在しないことは知っていますが、この状況をどのように解決するのか興味がありますか?
アイデアは、このサブスクリプションの「特性」を他のエンティティ (プロジェクト、チームなど) にも動的に拡張することです。
などのすべてのサブスクリプションに個別のテーブルを導入する必要がfeature_subscribers
ありissue_subscribers
ますか、またはよりエレガントな方法はありますか?
アップデート:
Subscription 側からターゲット オブジェクトのタイプを知りたくありません。User
エンティティ(機能、問題など)からサブスクライバー(のコレクション)を取得することだけを探しています。