php - Doctrine ManyToMany distinction field - possible? -


i'm trying achieve subscription model can applied multiple entities using single table/class doctrine 2. see explanation example below.

schema (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 

the table data this:

users: | id | name | | 1  | john | | 2  | joe  |  features: | id | name      | | 1  | feature | | 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 subscribed feature | 2  | issue       | 1         | 1        <- john subscribed issue 1 

what i'd expected have additional 'distinction' field can have in model's manytomany relation example:

manytomany:   subscribers:     targetentity: user     jointable:       name: subscriptions       joincolumns:         object_id:           referencedcolumnname: id         object_type:           value: feature 

i know latter soultion doesn't exist in doctrine, i'd curious how solve situation?

the idea dynamically extend subscription "trait" other entities (eg. project, team, etc)

do have introduce separate tables subscriptions feature_subscribers , issue_subscribers.. , on, or there more elegant way?

update:

i don't want know subscription side target object's type. i'm looking subscribers (collection of user) entities (feature, issue, etc).

you can achieve subscription table layout using single table inheritance discriminator map. ( see this blog post example )

lets subscription-manager service's subscribe(user $user, subscriptionobject $object) method receives user object , object subscribe ( feature or issue).

now subscription-manager creates either issuesubscription or featuresubscription object , persists it. way doctrine save object_type correctly depending on discriminator-map.

finally you'd have add listener/subscriber dynamically adjusts relation mapping ( relation issue or feature ) of ***subscription object in order have foreign key saved object_id.

a quicker way might using issuesubscription->issue , featuresubscription->feature relation mappings ending in table layout this:

subscriptions: | id | object_type | feature_id | issue_id  | user_id | 1  | feature     | 1          | null      | 1  | 2  | issue       | null       | 1         | 1  

... adding simple getobject() method basesubscription return ( $this->feature != null ) ? $this->feature : $this->issue; ommit need listener.


Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -