Doctrine 2 "Class does not exist" on persist

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;
}

and

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;

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

What am I missing here?

Answers


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.

So when the method

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

was called it was actually trying to save a number instead of the User Entity.

That is why the error "Class not found" was thrown, because there was no class "1".


You can't define relations on primary keys. Try to add another field in User class:

/**
 * @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;
}

Need Your Help

How to cut/crop/mask autoKrige result with boundary polygon?

r automap

I am busy developing a module that implements autoKrige (automap library) on soil sample data with a 20x20m cell-size. Once the kriging is complete I would like to crop/mask the kriging result with...

module selection in verilog

verilog system-verilog

I have a group of modules, say module_1, module_2, ... module_N. They perform similar yet different logic operations (out = logic_n). However, since the N is very large (thousands), it is unfeasibl...