如何整合多个实体的形式?Symfony 2


How to integrate forms of several entities? Symfony 2

我需要合并表'fos_user'和'institution'。我需要显示来自两个实体的注册表。我对FOSUserBundle有问题。我在用户类中创建了新属性

/**
 * @ORM'Id
 * @ORM'Column(type="integer")
 * @ORM'GeneratedValue(strategy="AUTO")
 */
protected $id;
protected $workPhone;
protected $adminPhone;
protected $name;
protected $adress;
public function __construct()
{
    parent::__construct();   
}
public function setAdress($adress)
{
    $this->adress = $adress;
}
 public function setName($name)
{
    $this->name = $name;
}
 public function setWorkPhone($workPhone)
{
    $this->workPhone = $workPhone;
}
 public function setAdminPhone($adminPhone)
{
    $this->adminPhone = $adminPhone;
}
public function getName()
{
    return $this->name;
}
public function getAdress()
{
    return $this->adress;
}
public function getWorkPhone()
{
    return $this->workPhone;
}
public function getAdminPhone()
{
    return $this->adminPhone;
}
/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}

我有实体机构,我想合并它。

/**
 * @var integer
 *
 * @ORM'Column(name="id_institution", type="integer")
 * @ORM'Id
 * @ORM'GeneratedValue(strategy="IDENTITY")
 */
private $idInstitution;
/**
 * @var string
 *
 * @ORM'Column(name="name", type="string", length=45, nullable=false)
 */
private $name;
/**
 * @var integer
 *
 * @ORM'Column(name="work_phone", type="integer", nullable=false)
 */
private $workPhone;
/**
 * @var integer
 *
 * @ORM'Column(name="admin_phone", type="integer", nullable=true)
 */
private $adminPhone;
/**
 * @var string
 *
 * @ORM'Column(name="adress", type="string", length=255, nullable=false)
 */
private $adress;
/**
 * @var 'AppBundle'Entity'FosUser
 *
 * @ORM'ManyToOne(targetEntity="AppBundle'Entity'FosUser")
 * @ORM'JoinColumns({
 *   @ORM'JoinColumn(name="id_fos_user", referencedColumnName="id")
 * })
 */
private $idFosUser;
/**
 * Get idInstitution
 *
 * @return integer 
 */
public function getIdInstitution()
{
    return $this->idInstitution;
}
/**
 * Set name
 *
 * @param string $name
 * @return Institution
 */
public function setName($name)
{
    $this->name = $name;
    return $this;
}
/**
 * Get name
 *
 * @return string 
 */
public function getName()
{
    return $this->name;
}
/**
 * Set workPhone
 *
 * @param integer $workPhone
 * @return Institution
 */
public function setWorkPhone($workPhone)
{
    $this->workPhone = $workPhone;
    return $this;
}
/**
 * Get workPhone
 *
 * @return integer 
 */
public function getWorkPhone()
{
    return $this->workPhone;
}
/**
 * Set adminPhone
 *
 * @param integer $adminPhone
 * @return Institution
 */
public function setAdminPhone($adminPhone)
{
    $this->adminPhone = $adminPhone;
    return $this;
}
/**
 * Get adminPhone
 *
 * @return integer 
 */
public function getAdminPhone()
{
    return $this->adminPhone;
}
/**
 * Set adress
 *
 * @param string $adress
 * @return Institution
 */
public function setAdress($adress)
{
    $this->adress = $adress;
    return $this;
}
/**
 * Get adress
 *
 * @return string 
 */
public function getAdress()
{
    return $this->adress;
}
/**
 * Set idFosUser
 *
 * @param 'AppBundle'Entity'FosUser $idFosUser
 * @return Institution
 */
public function setIdFosUser($idFosUser = null)
{
    $this->idFosUser = $idFosUser;
    return $this;
}
/**
 * Get idFosUser
 *
 * @return 'AppBundle'Entity'FosUser 
 */
public function getIdFosUser()
{
    return $this->idFosUser;
}

这是InstitutionManager的属性,我想保存实体,这就像现在的服务:

public function createNewEntity($user)
{
    $entity = new Institution();
    $entity->setName($user->getName());
    $entity->setAdminPhone($user->getAdminPhone());
    $entity->setWorkPhone($user->getWorkPhone());
    $entity->setAdress($user->getAdress());
    $entity->setidFosUser($user->getId());
    $em = $this->getDoctrine()->getManager();
    $em->persist($entity);
    $em->flush();
}

并听到覆盖注册控制器:

public function registerAction()
{
    $form = $this->container->get('fos_user.registration.form');
    $formHandler = $this->container->get('fos_user.registration.form.handler');
    $confirmationEnabled = $this->container->getParameter('fos_user.registration.confirmation.enabled');
    $process = $formHandler->process($confirmationEnabled);
    if ($process) {
        $user = $form->getData();
        $authUser = false;
        if ($confirmationEnabled) {
            $this->container->get('session')->set('fos_user_send_confirmation_email/email', $user->getEmail());
            $route = 'fos_user_registration_check_email';
        } else {
            $authUser = true;
            $route = 'fos_user_registration_confirmed';
        }
        $this->setFlash('fos_user_success', 'registration.flash.user_created');

        $institutionManager = $this->container->get('institution_manager');
        $institution = $institutionManager->createNewEntity($user);
        $url = $this->container->get('router')->generate($route);
        $response = new RedirectResponse($url);
        if ($authUser) {
            $this->authenticateUser($user, $response);
        }
        return $response;
    }
    return $this->container->get('templating')->renderResponse('FOSUserBundle:Registration:register.html.'.$this->getEngine(), array(
        'form' => $form->createView(),
    ));
}

services.yml:

services:
    institution_manager:
        class: AppBundle'Lib'Manager'InstitutionManager
        arguments: [ @doctrine.orm.default_entity_manager ]

您的InstitutionController未正确初始化。有一个setContainer方法,路由器调用它来设置容器。getDoctrine反过来需要容器,因此出现null对象错误。

一个简单的破解方法是自己调用setContainer方法:

$entity = new InstitutionController();
$entity->setContainer($this->container);
$entity->createNewEntity($user);

但这是一个黑客,你应该做一些重新设计。您所称的控制器根本不是控制器。这是一种服务,也许是一种工厂或经理。有点像FOS UserManager。

因此,请阅读如何定义服务:http://symfony.com/doc/current/book/service_container.html

这需要一些研究,但一旦你了解了这个过程,服务就会成为第二天性。您将把条令实体经理直接注入您的服务中。

class InstitutionManager
{
protected $entityManager;
public function __construct($entityManager)
{
    $this->entityManager = $entityManager;
}
public function createNewEntity($user)
{
    $entity = new Institution();
    $entity->setName($user->getName());
    $entity->setAdminPhone($user->getAdminPhone());
    $entity->setWorkPhone($user->getWorkPhone());
    $entity->setAdress($user->getAdress());
    $entity->setidFosUser($user->getId());
    $this->entityManager->persist($entity);
    $this->entityManager->flush();
    return $entity;
}

我会把服务交给你。实体管理器服务id为doctrine.orgm.default_entity_manager

你的控制器会看起来像:

$institutionManager = $this->get('institution_manager');
$institution = $institutionManager->create($user);

您还需要重新思考如何关联用户对象。userId的东西是否定的。你会想在$institution和$user之间建立一对一的关系。但这确实是一个不同的话题。

享受吧。