Reputation: 11
I'm using Zend Framework 2 with Doctrine2. I use a User and Project entity. A user can have more projects and a project can have multiple users. So i use another entity User_Project. They are all set up and I've validated the schema. All mapping files are correct and the database schema is in sync (orm:validate-schema). When I try to get all my users, I get an error that the target entity cannot be found (see title). Same error when I try getting one user.
ERROR
The target-entity Application\Entity\User_Project cannot be found in 'Application\Entity\User#user'.
Controller:
return $this->getEntityManager()->getRepository('Application\Entity\User')->findAll();
User entity
namespace Application\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/** @ORM\Entity
* @ORM\Table(name="user")
**/
class User {
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer", name="user_id")
*/
protected $user_id;
/** @ORM\Column(type="string") */
protected $fullName;
/** @ORM\OneToMany(targetEntity="User_Project", mappedBy="user") */
protected $user;
public function getUserId()
{
return $this->user_id;
}
public function getFullName()
{
return $this->fullName;
}
public function setFullName($value)
{
$this->fullName = $value;
}
}
Project entity
namespace Application\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/** @ORM\Entity
@ORM\Table(name="project")
**/
class Project {
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer", name="project_id")
*/
protected $project_id;
/** @ORM\Column(type="string") */
protected $customer;
/** @ORM\Column(type="string") */
protected $project_name;
/** @ORM\OneToMany(targetEntity="User_Project", mappedBy="project") */
private $project;
public function getProjectId()
{
return $this->project_id;
}
}
User_Project entity
namespace Application\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/** @ORM\Entity
* @ORM\Table(name="user_project")
**/
class User_Project {
/**
* @ORM\Id()
* @ORM\ManyToOne(targetEntity="User", inversedBy="user")
* @ORM\JoinColumn(name="user_id", referencedColumnName="user_id", nullable=false)
*/
protected $user;
/**
* @ORM\Id()
* @ORM\ManyToOne(targetEntity="Project", inversedBy="project")
* @ORM\JoinColumn(name="project_id", referencedColumnName="project_id", nullable=false)
*/
protected $project;
public function getUser()
{
return $this->project;
}
public function getProject()
{
return $this->user;
}
public function setUser($value)
{
$this->user = $value;
}
public function setProject($value)
{
$this->project = $value;
}
}
Modules.php
namespace Application;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
class Module
{
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
}
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
}
Upvotes: 1
Views: 523
Reputation: 530
may definition of your entity in Module.php or module.config.php is wrong . please copy Module.php or module.config.php
correct definition in module.config.php:
return array(
'doctrine' => array(
'driver' => array(
__NAMESPACE__ . '_driver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity')
),
'orm_default' => array(
'drivers' => array(
__NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'
))))),
in user Entity may be better rename user property to userProjects or projects and apply change on relation definition on User_Project
Upvotes: 1
Reputation: 530
check your column names in database . if you want have correct db . i recommend schema tool for create db .
$tool = new \Doctrine\ORM\Tools\SchemaTool($this->getEntityManager());
$classes = array(
$this->getEntityManager()->getClassMetadata('Application\Entity\User'),
$this->getEntityManager()->getClassMetadata('Application\Entity\Project'),
$this->getEntityManager()->getClassMetadata('Application\Entity\User_Project'),
);
$tool->dropSchema($classes);
$tool->createSchema($classes);
in a controller or somewhere else .
Upvotes: 0