2013-07-01 4 views
0

Est-il possible de créer un champ externe dans Association dans Doctrine2. L'objectif principal est d'avoir un type d'association. Par exemple, Nous avons des contacts et des opportunités. J'ai besoin de l'association entre Contacts et Opportunités avec un type de cette association.Doctrine2 - Association plusieurs-à-plusieurs avec le type d'association

Exemple de données:

contact_id | opportunity_id | association_type 
------------------------------------------------------ 
<contact_id> | <opportunity_id> | <Executive Sponsor>  
<contact_id> | <opportunity_id> | <Business Evaluator> 

Est-il possible de mettre en œuvre Doctrine2?

Voici mon association (YAML):

Opportunity: 
    type: entity 
    table: opportinity 
    ... 
    ... 
    ... 
    manyToMany: 
    contacts: 
     targetEntity: Contact 
     joinTable: 
     name: opportinities_contacts 
     joinColumns: 
      opportunity_id: 
      referencedColumnName: id 
     inverseJoinColumns: 
      contact_id: 
      referencedColumnName: id 

Merci

+0

double possible de [Doctrine2: La meilleure façon de gérer un grand nombre à plusieurs avec des colonnes supplémentaires dans le tableau de référence] (http://stackoverflow.com/questions/3542243/doctrine2-best-way-to-handle -many-to-many-avec-extra-columns-in-reference-table) – Crozin

Répondre

1

La meilleure pratique dans ce cas est de créer une association entité de classe.

Fondamentalement, diviser votre Many-to-many dans une paire de Many-to-one avec une nouvelle classe inbetween

Créer une nouvelle classe "ContactOpportunities" (Dans mon organisation, nous les nommons ToMap => ContactToOpportunityMap qui se trouve entre les classes

class ContactOpportunity { 

    /** 
    * @var <FQN>\Contact 
    * 
    * @ORM\Id 
    * @ORM\ManyToOne(targetEntity="<FQN>\Contact", inversedBy='opportunities') 
    * @ORM\JoinColumns({ 
    * @ORM\JoinColumn(name='Contact_ID', referencedColumnName="id") 
    * }) 
    protected $contact; 


    /** 
    * @var <FQN>\Opportunity 
    * 
    * @ORM\Id 
    * @ORM\ManyToOne(targetEntity="<FQN>\Opportunity", inversedBy='contacts') 
    * @ORM\JoinColumns({ 
    * @ORM\JoinColumn(name='Opportunity_ID', referencedColumnName="id") 
    * }) 
    protected $opportunity; 

    /* 
    * @var string type 
    * 
    * @ORM\Column(name="Association_Type", type="string") 
    protected $type; 
} 

Ou dans YML ...

ContactOpportunity 
    type: entity 
    table: opportunities_contacts 
    ... 
    ... 
    ... 
    manyToOne: 
    targetEntity: Contact 
    inversedBy: opportunities 
    joinColumn: 
     name: contact_id 
     referencedColumnName: id 
    manyToOne: 
    targetEntity: Opportunity 
    inversedBy: contacts 
    joinColumn: 
     name: opportunity_id 
     referencedColumnName: id 

convertir ensuite vos classes existantes pour cibler cette nouvelle classe.

Opportunity: 
    type: entity 
    table: opportunity 
    ... 
    ... 
    ... 
    oneToMany: 
    contacts: 
     targetEntity: ContactOpportunity 
     mappedBy: opportunity 

Contact: 
    type: entity 
    table: contact 
    ... 
    ... 
    ... 
    oneToMany: 
    opportunities: 
     targetEntity: ContactOpportunity 
     mappedBy: contact 
+0

Merci beaucoup. C'est très utile pour moi. – user2539253

+0

Si c'est utile, Upvote ou sélectionnez comme réponse s'il vous plaît :-) – Fodagus