Domanda

I'm trying to implement a repository pattern in my zend framework 2 application. I have made a service

<?php
class UserService {

private $userRepository;
public function __construct(IUserRepository $repo) {
    $this -> userRepository = $repo;
}

public function createUser($params) {
    $this -> userRepository -> create($params);
}

public function findAllUsers() {
    return $this -> userRepository -> getAllUsers();
}
}

which has a repository that implements an interface

class UserRepository implements IUserRepository {

public function getAllUsers() {
    //return all users
}

public function getUserById($id) {

}
public function getOneUser($params){

}
public function getUsers($params){

}
public function create($params){

}
public function update($params){

}
public function delete($params){

}
}


<?php
interface IUserRepository {

public function getAllUsers();
public function getUserById($id);
public function getOneUser($params);
public function getUsers($params);
public function create($params);
public function update($params);
public function delete($params);

}

In my module.php I make use of dependency injection to determine which repository I inject into a controller

public function getControllerConfig() {
    return array('factories' => array(
        'My\Controller\Accounts' => function(){
            return new AccountsController(new UserRepository());
        }, 
    ), 
    );
}

In my controller I pass the repository to my service

class AccountsController extends AbstractActionController {

private $service;
public function __construct(IUserRepository $repo) {
    $this->service = new UserService($repo);
}
public function indexAction() {
    $all_users = $this->service->findAllUsers();
    return new ViewModel(array('users' => $all_users));
}
}

My problem is that I'm using Doctrine as Orm and I want to use the entitymanager in my repositories but I don't know how to do that, any ideas and feedback are appreciated

È stato utile?

Soluzione

There are several ways to do this, of course. The typical way you'd do this kind of thing in a ZF2/D2 project would be to start with DoctrineORMModule.

That module exposes Doctrine's EntityManager via the ZF2 Service Manager in a variety of handy ways (you can $sm->get('doctrine.entitymanager.orm_default') to explicitly get the EM instance).

Once you can get your entitymanager from the SM, you write a factory for your repository, and inject the EM.


That said, there's a cleaner way. Doctrine has built-in support for repositories, and you can extend the default implementation.

Your repository would then look like this:

<?php
use Doctrine\ORM\EntityRepository;

class UserRepository extends EntityRepository implements IUserRepository {


public function getAllUsers() {
    return $this->findAll();
}

// ...
}

Just remember to add the repository class to the User Entity's metadata. For example, with an annotation:

/**
 * @ORM\Entity(repositoryClass="MyDomain\Model\UserRepository")
 */
class User
{

}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top