我正在使用zf2和doctrine,我已经像这样配置了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'
)
)
)
),
和我的LoginController
class LoginController extends AbstractRestfulController {
public function indexAction() {
$em = $this->getServiceLocator()-> get('Doctrine\ORM\EntityManager');
$usr = new User();
$usr->setUsername('yassine');
$usr->setPassword('yassine');
$usr->setEmail('yassine@gmail.com');
$em->persist($usr);
$em->flush(); } }
和我的班级用户/登录/实体/用户
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="user")
*/
class User
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(name="id")
*/
protected $id;
/**
* @ORM\Column(name="username")
*/
protected $username;
/**
* @ORM\Column(name="password")
*/
protected $password;
/**
* @ORM\Column(name="email")
*/
protected $email;
// Returns ID of the user
public function getId()
{
return $this->id;
}
// Sets ID of the user.
public function setId($id)
{
$this->id = $id;
}
// Returns username.
public function getUsername()
{
return $this->username;
}
// Sets username.
public function setUsername($username)
{
$this->username = $username;
}
// Returns password.
public function getPassword()
{
return $this->password;
}
// Sets Password.
public function setPassword($password)
{
$this->password = $password;
}
// Sets email.
public function setEmail($email)
{
$this->email = $email;
}
// Returns email.
public function getEmail()
{
return $this->email;
}
}
它向我显示此消息映射异常的问题:
课程'登录\实体\用户'在链配置的名称空间\ Entity
中找不到答案 0 :(得分:0)
__NAMESPACE__
似乎module.config.php
为空,这就是Doctrine看到以下内容的原因:
'drivers' => [
'\Entity' => '_driver'
]
而不是:
'drivers' => [
'Login\Entity' => 'Login_driver'
]
要解决此问题,您必须声明module.config.php
中使用的namespace。换句话说,请填写以下内容:
namespace Login;
位于文件顶部,正好在PHP打开标记之后。
另一种解决方案是用__NAMESPACE__
字符串替换所有'Login'
次出现。