Symfony Doctrine多对零关系

时间:2017-08-07 12:59:56

标签: php mysql symfony doctrine-orm doctrine

我是Symfony和Doctrine的新手,我不确定是否可以这样做。我可以在平板php中做到这一点,但似乎无法在Symfony / Doctrine中找到一种方法。我已经完成了大量的Stack Overflow问题并且搜索了Google,但没有任何乐趣。

我正在开展一个项目,该项目可以保存有关人员的基本信息,例如他们的姓名和头衔等。它还可以保存有关其雇主/组织的信息。它有两个实体/表。第一个叫做CrmPerson(用于存储有关此人的信息)。该表有一个名为personid的主键和一个名为orgid的外键。第二个表CrmOrg用于存储有关其组织的数据。该表有一个名为orgid的主键,没有外键。

许多人可以为一个组织工作,一个组织可以有很多人。即多对一。我已经能够构建一个表单,这样当您输入有关该人及其组织的信息时,当您点击提交时,它会将人员信息提交给CrmPerson表并将他们的组织信息提交给CrmOrg表。

但是,我有一个问题。在某些情况下,一个人可能根本不属于某个组织。多到零(我想......如果有这样的话!)。当人员信息输入表格但没有输入组织信息时,它会将人员的信息作为新记录提交给CrmPerson表(如预期的那样),但它也会在CrmOrg表中提交一个空记录,其中没有任何数据。除了主键orgid之外的字段(如预期但不想要)。外键orgid也填充在CrmPerson表中,该表链接到CrmOrg中的新空行。这可能会导致CrmOrg中数百/数千个空记录。

是否有可能实现这一点,以便如果将个人信息添加到表单但没有公司信息,则只向CrmPerson表提交记录,包括外键orgid的空/ null值以及不添加任何内容到了CrmOrg表呢?

以下是我正在尝试执行此操作的代码(删除了与问题无关的部分):

CrmOrg.php

<?php
// src/AppBundle/Entity/CrmOrg.php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
  * CrmOrg
  *
 * @ORM\Table(name="crm_org", indexes={@ORM\Index(name="index_org", columns=
 {"orgid", "memberid"})})
 * @ORM\Entity
 */
class CrmOrg
{
    /**
     * @var integer
     *
     * @ORM\Column(name="orgid", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $orgid;

    /**
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\CrmPerson", 
mappedBy="crmorg")
     */
    protected $crmpersons;

    /**
     * CrmOrg constructor.
     */
    public function __construct()
    {
        $this->crmpersons = new ArrayCollection();
    }

CrmPerson.php

/**
 * @var integer
 *
 * @ORM\Column(name="personid", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $personid;

/**
 * @var \AppBundle\Entity\CrmOrg
 *
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\CrmOrg")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="orgid", referencedColumnName="orgid")
 * })
 */
private $orgid;



/**
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\CrmOrg", inversedBy="crmpersons", cascade={"persist"})
 * @ORM\JoinColumn(name="orgid", referencedColumnName="orgid", nullable=false)
 */
protected $crmorg;

PersonController.php

<?php
// src/AppBundle/Entity/PersonController.php

namespace AppBundle\Controller;

use AppBundle\Form\CrmPersonType;
use AppBundle\Entity\CrmPerson;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Doctrine\ORM\EntityManagerInterface;




class PersonController extends Controller
{

    /**
     * @var EntityManagerInterface
     */
    private $em;

    public function __construct(EntityManagerInterface $em)
    {
        $this->em = $em;
    }

/**
 * @Route("/user/person_add", name="person_add")
 */
public function personAddAction(Request $request)
{
    $form = $this->createForm(CrmPersonType::class);



    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {

        $em = $this->getDoctrine()->getManager();

        $crmperson = $form->getData();

        $em->persist($crmperson);
        $em->flush();

        return $this->redirectToRoute('lead');
    }

    return $this->render(':lead:lead.add.html.twig', [
        'leadForm' => $form->createView()
    ]);
}

CrmPersonType

<?php
// src/AppBundle/Entity/CrmPersonType.php


namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use AppBundle\Entity\CrmPerson;
use AppBundle\Entity\CrmOrg;
use AppBundle\Form\CrmOrgType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;


class CrmPersonType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('title', ChoiceType::class, array(
                  'choices'  => array(
                        'Mr' => 'Mr',
                        'Miss' => 'Miss',
                        'Mrs' => 'Mrs',
                        'Ms' => 'Ms',

                                    ),
                  'required'   => false,
            ))

            ->add('firstName', TextType::class, array(
                  'required'   => false,
                  )) 
            ->add('middleNames', TextType::class, array(
                  'required'   => false,
                  )) 
            ->add('surname', TextType::class, array(
                  'required'   => false,
                  ))
            ->add('tel', TextType::class, array(
                  'required'   => false,
                  ))
            ->add('mob', TextType::class, array(
                  'required'   => false,
                  ))
            ->add('email', TextType::class, array(
                  'required'   => false,
                  ))
            ->add('jobTitle', TextType::class, array(
                  'required'   => false,
                  ))

            ->add('crmorg', CrmOrgType::class)
            ->add('submit', SubmitType::class, [
                'label' => 'Save',
                'attr'  => [
                    'class' => 'btn btn-success'
                ]
            ])
        ;
    }

CrmOrgType

<?php
// src/AppBundle/Entity/CrmPersonType.php

namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;

class CrmOrgType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('companyName', TextType::class, array(
                  'required'   => false,
                  ))
            ->add('companyWebsite', TextType::class, array(
                  'required'   => false,
                  ))
            ->add('industry', TextType::class, array(
                  'required'   => false,
                  )) 


        ;
    }

1 个答案:

答案 0 :(得分:0)

@JoinColumn的{​​{1}}注释中,添加$orgid参数:

nullable=true

这样,您就不必将用户与任何组织相关联。

顺便说一句,如果您只有一个连接列,则无需在@ORM\JoinColumn(name="orgid", referencedColumnName="orgid", nullable=true) 内放置@JoinColumn注释。您只需将@JoinColumns注释放在@JoinColumn下方即可。

相关问题