I started to develop new application, it’s my first application written in ZF2. I have some experience about making application with ZF1 with Doctrine1, but now I would like to do application with using ZF2 and Doctrine2.
Default entities folder is in the module directory. I would like to have one models folder, which is shared in whole application. So I’ve prepared application structure like this:
config
models
generated
module
Administration
Application
public
vendor
I have designed database (MySQL) and imported it to XML form, using Doctrine CLI. I’ve generated also models. But now I have problem – how to use it in my modules? I’ve prepared some code:
protected $em;
public function setEntityManager(EntityManager $em)
{
$this->em = $em;
}
public function getEntityManager()
{
if (null === $this->em) {
$this->em = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
}
return $this->em;
}
public function indexAction()
{
return new ViewModel(array(
'carriers' => $this->getEntityManager()->getRepository('Carrier')->findAll()
));
}
And I’ve got an error ‘Class Carrier not found’. It is a problem with paths, but I’m totally newbie in ZF2 and I’ve no idea how to configure it…
You define the path to Doctrine 2’s entities within configuration. Now i assume you have a configuration somewhere similar to this:
Basically what you have to do is to Map the paths correctly. Within the
orm_defaultsyou can define the driver for each module separately, or just use a global driver.Within
__NAMESPACE__ . '_driver'you define a driver with the given name and also define where exactly the entities get stored. In your case this would probably be something likeOr whatever your Module is named where your entities are stored. In this case
AdministrationAdditionally, always assign the fully qualified repository name like this: