Custom repository for Doctrine and ZF2

2.2k views Asked by At

I'm using ZF2 with Doctrine and I would like to use a repository but I've this error:

The class 'Application\Repository\EventRepository' was not found in the chain configured namespaces Application\Entity

The entity:

namespace Application\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="Application\Repository\EventRepository")
 */
class Event {
    // ...
}

The repository:

namespace Application\Repository;

use Doctrine\ORM\EntityRepository;

class EventRepository extends EntityRepository
{
    public function test()
    {
        // ...
    }
}

In the controller:

$this->getEntityManager()->getRepository('Application\Repository\EventRepository')->test()

In my module.config.php, I've this:

return array(
    // ...
    'doctrine' => array(
        'driver' => array(
            'application_entities' => array(
                'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
                'cache' => 'array',
                'paths' => array(__DIR__ . '/../src/Application/Entity')
            ),
            'orm_default' => array(
                'drivers' => array(
                    'Application\Entity' => 'application_entities'
                )
            )
        ),
        // ...
    )
);

So I tried to change for:

return array(
    // ...
    'doctrine' => array(
        'driver' => array(
        'application_entities' => array(
            'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
            'cache' => 'array',
            'paths' => array(__DIR__ . '/../src/Application/Entity')
        ),
        'application_repositories' => array(
            'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
            'cache' => 'array',
            'paths' => array(__DIR__ . '/../src/Application/Repository')
        ),
        'orm_default' => array(
            'drivers' => array(
                'Application\Entity'     => 'application_entities',
                'Application\Repository' => 'application_repositories'
            )
        )
    ),
        // ...
    )
);

And I've a new error:

Class "Application\Repository\EventRepository" sub class of "Doctrine\ORM\EntityRepository" is not a valid entity or mapped super class.

What is the problem?

Thank you

2

There are 2 answers

0
SylarBg On

Check this Custom Repositories You must invoke $this->getEntityManager()->getRepository('Application\Entity\Event')->test()

0
Omkar Bandkar On

Yes skurty, SylarBg is right instead of calling the repository directly, you need to call the related entity through which the call is sent to the repository