如何在Symfony2中处理实体的主/辅助/正常状态

I am developing an application and I came across the following: Lets say I have an entity called Contact, that Contact belongs to a Company and the Company has a Primary Contact and a Secondary Contact and also has the remaining Contacts which I've named Normal.

My question is, what is the best approach for this when talking about entities properties and also form handling. I've though about two things:

  1. Having 2 fields on the Company entity called PrimaryContact and SecondaryContact and also have a one-to-many relationship to a property called contacts.

What I don't like (or I'm not 100% how to do) about this option is that on the Contact entity I would need an inversedBy field for each of the 2 one-to-one properties and also 1 for the one-to-many relationship and my personal thought is that this is kind of messy for the purpose.

  1. Having a property on the Contact entity called Type which would hold if it's primary, secondary or normal and in the Company methods that has to do with Contacts I would modify it and add the getPrimaryContact, getSecondaryContact, etc.

What I don't like about this option is that I would need to have 2 unmapped properties for the Company and I would need to do a lot on the form types in order to get this to work smoothly.

My question is what is the best approach for this structure and how to deal with forms and these dependencies. Let me know if this is not clear enough and I will take time and preparate an example with code and images.

Thanks to @Cerad this is the following approach I took:

  1. I have a OneToMany property on the Company to hold all the contacts.
  2. Implemented the getPrimaryContact/setPrimaryContact methods and looped through all the contacts and retrieving the one of the type I want. Did the same for the secondary.
  3. On the Form type of the company my issue was that I had the 'mapped' => 'false' option, I removed this since I implemented the getters and setters SF2 knows it has to go to these methods.

`

<?php

namespace XYZ\Entity;

/**
 * @ORM\Entity
 * @ORM\HasLifecycleCallbacks()
 */
class Company
{
    ... 
    /**
     * @ORM\OneToMany(targetEntity="\XYZ\Entity\Contact", mappedBy="company", cascade={"persist", "remove"})
     */
    private $contacts;

    public function getPrimaryContact() { ... }
    public function setPrimaryContact(Contact $contact) { //Set the type of $contact and add it $this->addContact($contact) }
    public function getSecondaryContact() { ... }    
    public function setSecondaryContact(Contact $contact) { //Set the type of $contact and add it $this->addContact($contact) }

}`

And for the Form Type I have:

`

class CompanyType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {

        $builder
            ...
            ->add('primaryContact', new ContactType())
            ->add('secondaryContact', new ContactType())
    }

    ...
}`

With this set everything runs smoothly and I can CRUD without much struggle.

I'm not yet a Symfony expert but i'm currently learning entites manipulation and relations ! And there is not simple way to do relations with attributes.

You have to create an entity that represent your relation.

Let's suppose you have an entity Company and and entity Contact

Then you will have an entity named CompanyContact whick will represent the relation between your objects. (you can have as many attributes as you wish in your relation entity). (Not sure for the Many-to-One for your case but the idea is the same)

<?php

namespace My\Namespace\Entity

use Doctrine\ORM\Mapping as ORM

/**
 * @ORM\Entity(repositoryClass="My\Namespace\Entity\CompanyContactRepository")
 */
class CompanyContact
{
  /**
   * @ORM\Column(name="id", type="integer")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  private $id;

  /**
   * @ORM\Column(name="contact_type", type="string", length=255)
   */
  private $contactType;

  /**
   * @ORM\ManyToOne(targetEntity="My\Namespace\Entity\Company")
   * @ORM\JoinColumn(nullable=false)
   */
  private $company;

  /**
   * @ORM\ManyToOne(targetEntity="My\Namespace\Entity\Contact")
   * @ORM\JoinColumn(nullable=false)
   */
  private $contact;

}

And in your controller you can do this:

$em = $this->getDoctrine()->getManager();
$company            = $em->getRepository('YourBundle:Company')->find($yourCompanyId);
$yourType           = "primary";
$companyContacts    = $em->getRepository('YourBundle:CompanyContact')
                         ->findBy(array('company' => $company, 'type' => $yourType));

What do you think about this approach ? If i learn more soon i will get you posted ;)