【发布时间】:2014-01-02 04:01:10
【问题描述】:
我正在尝试将 Doctrine 2 ORM 用于我的 Zend2 应用程序之一。我在 composer 的帮助下使用 Doctrine 模块在应用程序中进行了设置。
我能够将数据持久化到数据库中,但是当我在对象管理器上进行 find() 调用时,它给了我一个映射异常,并显示以下消息。
Doctrine\Common\Persistence\Mapping\MappingException
File:
/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/MappingException.php:96
Message:
Class 'User' does not exist
以下是在Application模块配置文件下添加的Doctrine设置
'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'
)
)
)
),
这是在 Application\src\Entity 文件夹下创建的用户实体
<?php
namespace Application\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class User {
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="NONE")
* @var int
*/
protected $user_id;
/**
* @ORM\Column(type="integer")
* @var int
*/
protected $network_id;
/**
* @ORM\Column(type="string", length=64)
* @var string
*/
protected $network_name;
/**
* @ORM\Column(type="string", length=64)
* @var string
*/
protected $job_title;
/**
* @ORM\Column(type="string", length=64)
* @var string
*/
protected $location;
/**
* @ORM\Column(type="string", length=64)
* @var string
*/
protected $first_name;
/**
* @ORM\Column(type="string", length=64)
* @var string
*/
protected $last_name;
/**
* @ORM\Column(type="string", length=255)
* @var string
*/
protected $url;
/**
* @ORM\Column(type="string", length=255)
* @var string
*/
protected $img_url;
/**
* @ORM\Column(type="string", length=255)
* @var string
*/
protected $department;
/**
* @ORM\Column(type="string", length=255)
* @var string
*/
protected $email_address;
/**
* @ORM\Column(type="boolean")
* @var string
*/
protected $verified;
/**
* @return the int
*/
public function getUserId() {
return $this->user_id;
}
/**
* @param int $user_id
*/
public function setUserId($user_id) {
$this->user_id = $user_id;
return $this;
}
/**
* @return the int
*/
public function getNetworkId() {
return $this->network_id;
}
/**
* @param int $network_id
*/
public function setNetworkId($network_id) {
$this->network_id = $network_id;
return $this;
}
/**
* @return the string
*/
public function getNetworkName() {
return $this->network_name;
}
/**
* @param string $network_name
*/
public function setNetworkName($network_name) {
$this->network_name = $network_name;
return $this;
}
/**
* @return the string
*/
public function getJobTitle() {
return $this->job_title;
}
/**
* @param string $job_title
*/
public function setJobTitle($job_title) {
$this->job_title = $job_title;
return $this;
}
/**
* @return the string
*/
public function getLocation() {
return $this->location;
}
/**
* @param string $location
*/
public function setLocation($location) {
$this->location = $location;
return $this;
}
/**
* @return the string
*/
public function getFirstName() {
return $this->first_name;
}
/**
* @param string $first_name
*/
public function setFirstName($first_name) {
$this->first_name = $first_name;
return $this;
}
/**
* @return the string
*/
public function getLastName() {
return $this->last_name;
}
/**
* @param string $last_name
*/
public function setLastName($last_name) {
$this->last_name = $last_name;
return $this;
}
/**
* @return the string
*/
public function getUrl() {
return $this->url;
}
/**
* @param string $url
*/
public function setUrl($url) {
$this->url = $url;
return $this;
}
/**
* @return the string
*/
public function getImgUrl() {
return $this->img_url;
}
/**
* @param string $img_url
*/
public function setImgUrl($img_url) {
$this->img_url = $img_url;
return $this;
}
/**
* @return the string
*/
public function getDepartment() {
return $this->department;
}
/**
* @param string $department
*/
public function setDepartment($department) {
$this->department = $department;
return $this;
}
/**
* @return the string
*/
public function getEmailAddress() {
return $this->email_address;
}
/**
* @param string $email_address
*/
public function setEmailAddress($email_address) {
$this->email_address = $email_address;
return $this;
}
/**
* @return the boolean
*/
public function getVerified() {
return $this->verified;
}
/**
* @param boolean $verified
*/
public function setVerified($verfied) {
$this->verified = $verfied;
return $this;
}
}
现在,当我从 Application 模块的 IndexController 对上述实体进行持久操作时,它工作正常。但是当我在同一个 IndexController 中使用同一个对象映射器进行查找操作时,它给出了映射异常。
以下是我的做法:
$objectManager = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
$objectManager->persist($user);
$objectManager->flush();
$user = $objectManager->find('User', $uniqueID);
谁能帮我解决这个问题?
问候。
【问题讨论】:
标签: zend-framework orm doctrine-orm zend-framework2