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

时间:2015-02-20 16:16:41

标签: php symfony doctrine-orm fosuserbundle

我需要合并表'fos_user'和'institution'。我需要显示两个实体的注册表。 我有FOSUserBundle的问题。 我在User class

中创建了新属性
/**
 * @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();

}

并听取覆盖RegistrationController:

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 ]

1 个答案:

答案 0 :(得分:2)

您的InstitutationController未正确初始化。路由器调用了一个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;
}

我会把services.yml留给你。实体管理器服务标识为doctrine.orm.default_entity_manager

您的控制器将如下所示:

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

您还需要重新考虑如何关联用户对象。 userId的东西是不是没有。您将希望在$ institution和$ user之间建立一对一的关系。但这确实是一个不同的主题。

享受。