坚持主义2“阶级不存在”

时间:2012-06-01 09:18:25

标签: zend-framework doctrine-orm one-to-many many-to-one persist

所以我有两个类,它们之间有OneToMany和ManyToOne关系:

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时,on persist会抛出以下错误:

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()

我在这里缺少什么?

2 个答案:

答案 0 :(得分:2)

问题实际上是我试图保存的对象。我没有将整个User Entity传递给ownerId,只传递了用户的id。

所以当方法

Doctrine\ORM\UnitOfWork->doPersist(1, Array)

被称为实际上是在尝试保存数字而不是用户实体。

这就是抛出错误“Class not found”的原因,因为没有类“1”。

答案 1 :(得分:1)

您无法在主键上定义关系。尝试在User类中添加另一个字段:

/**
 * @Table(name="users")
 * @Entity
 * @author Csabi
 */
class User {

    /**
     * @Id
     * @Column(name="id", type="integer", nullable=false)
     * @GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @OneToMany(targetEntity="JobListing", mappedBy="ownerId", orphanRemoval=true)
     */
    private $jobListings;

    /**
     * @var string $displayname
     * @Column(type="string", nullable=false)
     */
    private $displayname;
}