1

I'm having a problem with passing the entity manager between two layers of controllers.

The system I'm building has the following structure:

2 Bundles:

Core Bundle (let's call it Backend Controller)

This is the bundle that contains all the Models (entities) and business rules/logic.

API Bundle (call it Frontend controller)

Is responsible for checking the permissions of passed in api keys and communicating with the Core bundle to get the info.

Here's an example with the User controllers and entities:

UserController.php in APIBundle:

<?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 in CoreBundle:

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

Please, tell me if I'm doing this right. It seems strange to pass the entity manager between the controllers every time.

Maybe there's a better way of doing that?

Does the idea between the separation of the bundles make sense?

Thank you, every idea is greatly appreciated.

4

2 回答 2

1

如果 CoreBundle UserController 从来没有通过 HTTP 访问过,它的方法也没有返回 Symfony\Component\HttpFoundation\Response 的实例,那么它就不是一个真正的控制器。

您最好将其定义为服务,如在 CoreBundle\Service\User 中,并通过 DI 容器注入 EntityManager。

服务.yml

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

然后它可以从 Acme\Bundle\APIBundle\Controller\UserController 获得,其中包含以下内容:

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

当然,你也可以将 Acme\Bundle\APIBundle\Controller\UserController 自己定义为服务,然后注入 'corebundle.userservice',方便。

我建议您阅读有关依赖注入的 Symfony 文档。

于 2014-02-24T13:55:22.353 回答
-1

在实体类中搜索获取实体管理器是错误的方法!

在 CoreBundle 中,您使用与实体类相同的 UserController.php。

阅读文档以了解如何在 symfony 中正确使用存储库。

在 APIBundle 的 UserController 中,您必须调用自定义存储库函数。此存储库在您的实体中声明。

于 2014-02-21T10:57:23.150 回答