douchuanchai2793 2015-02-20 16:16
浏览 24

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

I need to merge table 'fos_user' and 'institution'.And I need to display registration form from both entities. I have a problem with FOSUserBundle. I created new properties in 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;
}

And I have entity Institution, which I want merge.

/**
 * @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;
}

This is pert of InstitutionManager where I want save Entity, and this is like service now:

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();

}

And hear override 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条回答 默认 最新

  • dtkz3186 2015-02-20 21:39
    关注

    Your InstitutationController is not being properly initialized. There is a setContainer method which the router calls to, well, set the container. getDoctrine in turn needs the container, hence the null object error.

    A simple hack would be to call the setContainer method yourself:

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

    But it's a hack you should do some redesigning. What you are calling a controller is not a controller at all. It's a service, maybe a sort of a factory or manager. Sort of like the FOS UserManager.

    So read up on how to define a service: http://symfony.com/doc/current/book/service_container.html

    It takes a bit of research but once you understand the process then services will become second nature. You will inject the doctrine entity manager directly into your service.

    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;
    }
    

    I will leave services.yml up to you. The entity manager service id is doctrine.orm.default_entity_manager

    Your controller would then look like:

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

    You will also want to rethink how you are relating the user object. The userId stuff is a no no. You will want to make a one-to-one relation between $institution and $user. But that is really a different topic.

    Enjoy.

    评论

报告相同问题?

悬赏问题

  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了