2014-05-23 2 views
0

Symfony 2.3에서 FOSUserBundle을 사용하고 있습니다. 사용자 프로필을 저장하기위한 추가 테이블을 만들었습니다. 어느 시점에서 유지하고 플러시하기 위해 엔티티 관리자에 액세스해야합니다. 프로필 테이블에서 프로필 테이블로 오는 데이터가 있지만 엔티티 관리자의 인스턴스를 가져 오는 데 문제가 있습니다.FOSUserBundle 컨트롤러에서 Entity Manager의 인스턴스를 얻는 방법

아이디어가 있으십니까?

감사합니다.

+0

자세한 내용을 제공 할 수 있습니까? 예. 엔티티, 컨트롤러, FOSUserBundle 구성? – bartek

답변

2

FOSUserBundle을 사용하여 사용자 프로필을 편집하려면 FOSUserBundle의 ProfileController를 확장하는 새 ProfileController를 만들어야합니다.

이제 FOSUserBundle의 메서드를 재정의 할 수 있습니다.

시작의 exemple :

<?php 

/* 
* This file is part of the FOSUserBundle package. 
* 
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> 
* 
* For the full copyright and license information, please view the LICENSE 
* file that was distributed with this source code. 
*/ 

namespace Acme\UserBundle\Controller; 

use FOS\UserBundle\Model\UserInterface; 
use Symfony\Component\HttpFoundation\Request; 
use Symfony\Component\HttpFoundation\RedirectResponse; 
use Symfony\Component\Security\Core\Exception\AccessDeniedException; 
use FOS\UserBundle\Controller\ProfileController as BaseController; 

/** 
* Controller managing the user profile 
* 
* @author Christophe Coevoet <[email protected]> 
*/ 
class ProfileController extends BaseController 
{ 
    public function editAction() 
    { 
    $em = $this->container->get('doctrine')->getManager(); 
    // your code here 
    } 
} 
+0

이것은 [Event | 가입자] Listener를 사용하면 더 잘 처리 될 수 있습니다. FOSUserBundle의 ProfileController는 엔터티가 아직 플러시되지 않았기 때문에 사용자가 청취 할 수있는 여러 이벤트를 throw하므로 개체 (User)에 영향을 미치지 않고 Entity Manager에 액세스 할 필요가 없습니다. 더 많은 정보가 필요할 것입니다. 예를 들어, 다음과 같이 메소드는 객체를 가져 와서 변경하고 Entity Manager가 필요없이 이동합니다. https://github.com/Sylius/Sylius/blob/master/src /Sylius/Bundle/CoreBundle/EventListener/OrderPromotionListener.php – qooplmao

2

당신은 교리 서비스에이 방법으로 액세스 할 수 있습니다 다음 FOSUserBundle의

$em = $this->container->get('doctrine')->getManager(); 
1

이 컨트롤러는 Symfony\Component\DependencyInjection\ContainerAware에서 확장 할 수 있습니다. 따라서 Symfony\Component\DependencyInjection\ContainerInterface에서 $this->container에 액세스 할 수 있습니다. 이 컨테이너를 사용하면 다음과 같이 Doctrine 및 EntityManager에 순차적으로 액세스 할 수 있습니다.

관련 문제