教义2“类别不存在".坚持 [英] Doctrine 2 "Class does not exist" on persist
本文介绍了教义2“类别不存在".坚持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我有这两个类,它们之间具有OneToMany和ManyToOne关系:
So I have these two classes with OneToMany and ManyToOne relationships between them:
namespace RM\Entity;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @Table(name="users")
* @Entity
* @author Csabi
*/
class User {
/**
* @Id
* @Column(name="id", type="integer", nullable=false)
* @GeneratedValue(strategy="IDENTITY")
* @OneToMany(targetEntity="JobListing", mappedBy="ownerId", orphanRemoval=true)
*/
private $id;
/**
* @var string $displayname
* @Column(type="string", nullable=false)
*/
private $displayname;
}
和
namespace RM\Entity;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @Table(name="job_listings")
* @Entity(repositoryClass="RM\Entity\Repository\JobListingRepository")
* @author Csabi
*/
class JobListing {
/**
* @var integer $id
* @Column(name="id", type="integer", nullable=false)
* @Id
* @GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var User
* @ManyToOne(targetEntity="User",cascade={"persist"})
* @JoinColumns({
* @JoinColumn(name="ownerId", referencedColumnName="id", nullable=false)
* })
*/
private $ownerId;
但是,在保存JobListing时,如果持久保存,则会引发以下错误:
However, when saving a JobListing, on persist it throws the following error:
Application error
Exception information:
Message: Class does not exist
Stack trace:
#0 library\Doctrine\ORM\Mapping\ClassMetadata.php(67): ReflectionClass->__construct(false)
#1 library\Doctrine\ORM\Mapping\ClassMetadataFactory.php(350): Doctrine\ORM\Mapping\ClassMetadata->__construct(false)
#2 library\Doctrine\ORM\Mapping\ClassMetadataFactory.php(260): Doctrine\ORM\Mapping\ClassMetadataFactory->newClassMetadataInstance(false)
#3 library\Doctrine\ORM\Mapping\ClassMetadataFactory.php(169): Doctrine\ORM\Mapping\ClassMetadataFactory->loadMetadata(false)
#4 library\Doctrine\ORM\EntityManager.php(247): Doctrine\ORM\Mapping\ClassMetadataFactory->getMetadataFor(false)
#5 library\Doctrine\ORM\UnitOfWork.php(1222): Doctrine\ORM\EntityManager->getClassMetadata(false)
#6 library\Doctrine\ORM\UnitOfWork.php(1678): Doctrine\ORM\UnitOfWork->doPersist(1, Array)
#7 library\Doctrine\ORM\UnitOfWork.php(1252): Doctrine\ORM\UnitOfWork->cascadePersist(Object(RM\Entity\JobListing), Array)
#8 library\Doctrine\ORM\UnitOfWork.php(1201): Doctrine\ORM\UnitOfWork->doPersist(Object(RM\Entity\JobListing), Array)
#9 library\Doctrine\ORM\EntityManager.php(454): Doctrine\ORM\UnitOfWork->persist(Object(RM\Entity\JobListing))
#10 library\RM\Entity\JobListingService.php(54): Doctrine\ORM\EntityManager->persist(Object(RM\Entity\JobListing))
#11 application\controllers\JobListingController.php(28): RM\Entity\JobListingService->saveJobListing(Object(RM\Entity\JobListing))
#12 library\Zend\Controller\Action.php(516): JobListingController->addAction()
我在这里想念什么?
推荐答案
问题实际上出在我要保存的对象中.我没有将整个用户实体传递给ownerId,只有用户的ID.
The problem was actually in the object I was trying to save. I did not pass the whole User Entity to the ownerId, only the id of the user.
所以当方法
Doctrine\ORM\UnitOfWork->doPersist(1, Array)
被称为实际上是在尝试保存数字而不是用户实体.
was called it was actually trying to save a number instead of the User Entity.
这就是为什么抛出错误找不到类"的原因,因为没有类"1".
That is why the error "Class not found" was thrown, because there was no class "1".
这篇关于教义2“类别不存在".坚持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文