简体   繁体   English

如何在Symfony2中处理实体的主要/次要/普通状态

[英]How to handle Primary/Secondary/Normal statuses for entity in 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. 我正在开发一个应用程序,遇到了以下问题:假设我有一个名为Contact的实体,该Contact属于公司,并且Company有一个主要联系人和一个次要联系人,还有我称为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. 在Company实体上有2个字段,分别称为PrimaryContact和SecondaryContact,并且还与称为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. 我不喜欢(或者我不是100%怎么做)关于此选项的原因是,在Contact实体上,我需要对2个一对一属性中的每一个都需要一个inversedBy字段,对于一个属性也需要1一对多的关系,我个人认为这对我来说是一团糟。

  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. 在Contact实体上有一个称为Type的属性,如果该属性是主要的,辅助的或普通的,并且在与Contacts有关的Company方法中可以保存,我将对其进行修改并添加getPrimaryContact,getSecondaryContact等。

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. 我对该选项不满意的是,我需要为该公司拥有2个未映射的属性,并且我需要对表单类型做很多事情才能使其顺利运行。

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. 让我知道这是否还不够清楚,我将花一些时间准备一个包含代码和图像的示例。

I'm not yet a Symfony expert but i'm currently learning entites manipulation and relations ! 我还不是Symfony专家,但是我正在学习如何操纵和建立关系! 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. 然后,您将有一个名为CompanyContact的实体,它将代表您的对象之间的关系。 (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 ;) 如果我很快能学到更多,我会把你发布出去的;)

Thanks to @Cerad this is the following approach I took: 感谢@Cerad,这是我采用的以下方法:

  1. I have a OneToMany property on the Company to hold all the contacts. 我在公司上拥有一个OneToMany属性,可以容纳所有联系人。
  2. Implemented the getPrimaryContact/setPrimaryContact methods and looped through all the contacts and retrieving the one of the type I want. 实现了getPrimaryContact / setPrimaryContact方法并遍历所有联系人并检索我想要的一种类型。 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. 在公司的Form类型上,我的问题是我拥有'mapped'=>'false'选项,我删除了此选项,因为我实现了getter和setter SF2知道必须使用这些方法。

` `

<?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. 有了这个设置,一切都会顺利进行,而我可以轻松进行CRUD。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM