2014-02-20 2 views
1

컨트롤러의 두 레이어간에 엔티티 관리자를 전달하는 데 문제가 있습니다.Symfony 2.4.2 - 다른 번들의 컨트롤러간에 엔티티 관리자 전달

난 건물은 다음과 같은 구조를 가지고있어 시스템 :

2 개 번들 :

코어 번들

이 모든 모델이 들어있는 번들입니다 (의 백엔드 컨트롤러를 호출하자) (엔터티) 및 비즈니스 규칙/논리.

API 번들 (호출 프런트 엔드 컨트롤러)

가의 권한을 확인하기위한 책임인가 API 키를 전달하고 정보를 얻을 수있는 핵심 번들과 통신. ,

<?php 

namespace Acme\Bundle\CoreBundle\Controller; 

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; 
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; 
use Symfony\Component\HttpFoundation\Response; 

use Acme\Bundle\CoreBundle\Entity\User; 

class UserController extends BaseController 
{ 

    function __construct($em) { 
     parent::__construct($em); 
     $this->entity = new User(); 
    } 

    /** 
    * Get userId 
    * 
    * @return integer 
    */ 
    public function getUserId() 
    { 
     return $this->entity->userId; 
    } 

    /** 
    * Set firstName 
    * 
    * @param string $firstName 
    * @return User 
    */ 
    public function setFirstName($firstName) 
    { 
     $this->entity->firstName = $firstName; 

     return $this; 
    } 

    // ... 
    public function load($id) { 

    if (!$this->entity instanceof \Acme\Bundle\CoreBundle\Entity\BaseEntity) { 
     throw new \Exception('invalid entity argument'); 
    } 

    $this->entity = $this->em->getRepository('AcmeCoreBundle:User')->find($id); 
    } 
} 

하십시오 APIBundle에서

UserController.php : CoreBundle에서

<?php 

namespace Acme\Bundle\APIBundle\Controller; 

use Symfony\Component\HttpFoundation\Response; 
use Symfony\Component\HttpFoundation\Request; 

use Acme\Bundle\CoreBundle\Controller\UserController as User; 


use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; 

class UserController extends BaseController implements AuthenticatedController 
{ 

    public function readAction(Request $request) { 

     $user = new User($this->getDoctrine()->getManager()); 

     $user->load(2); 

     return $this->response($user); 
    } 
} 

UserController.php

다음은 사용자 컨트롤러 및 기관과 예입니다 내가이 일을 제대로하고 있는지 말해줘. 매번 컨트롤러간에 엔티티 관리자를 전달하는 것이 이상하게 보입니다.

어쩌면 더 좋은 방법이 있을까요?

번들 분리 사이의 아이디어가 맞습니까?

감사합니다. 모든 아이디어에 크게 감사드립니다.

답변

1

CoreBundle UserController가 HTTP를 통해 액세스되지 않고 해당 메서드가 Symfony \ Component \ HttpFoundation \ Response의 인스턴스를 반환하지 않으면 실제로 컨트롤러가 아닙니다.

CoreBundle \ Service \ User 에서처럼 서비스로 정의하고 DI 컨테이너를 통해 EntityManager를 주입해야합니다.

sevices.YML

corebundle.userservice: 
    class: Acme\CoreBundle\Service\User 
    arguments: [@doctrine.orm.entity_manager] 

은 다음과 한국 전기 \ 번들 \ APIBundle \ 컨트롤러 \ UserController에서 사용할 수 있습니다 : 물론

$user = $this->get('corebundle.userservice'); 

, 당신은 또한 한국 전기 \ 번들 \ APIBundle \ 컨트롤러 \ UserController를 정의 할 수 있습니다 편의성을 위해 'corebundle.userservice'를 삽입하십시오.

Dependency Injection에있는 Symfony 문서를 읽어 보시기 바랍니다.

-1

엔티티 클래스의 엔티티 관리자를 검색하면 잘못된 방법입니다!

CoreBundle에서는 UserController.php를 Entity 클래스와 동일하게 사용합니다.

을 읽으면 심포니에서 저장소를 올바르게 사용하는 방법을 이해할 수 있습니다.

APIBundle의 UserController에서 맞춤 저장소 기능을 호출해야합니다. 이 저장소는 엔티티에 선언되어 있습니다.

+0

저는 비즈니스 논리를 모델에 적용하는 것이 정말 편하지 않습니다. –

관련 문제