0

不知道我在哪里出错了。刚接触 Doctrine,它可能很简单。

我试图将一个新的注册用户保存到 Account 表中,然后在相关的 Profile 表中创建一个条目,然后将 Profile id 存储在 Accounts profile_id 字段中。

我得到的错误是

Application error
Exception information:

Message: Class does not exist
Stack trace:

#0 /Library/Server/Web/Data/Sites/euphoriagaming/public_html/library/Doctrine/ORM/Mapping/ClassMetadata.php(67): ReflectionClass->__construct('')
#1 /Library/Server/Web/Data/Sites/euphoriagaming/public_html/library/Doctrine/ORM/Mapping/ClassMetadataFactory.php(350): Doctrine\ORM\Mapping\ClassMetadata->__construct(false)
#2 /Library/Server/Web/Data/Sites/euphoriagaming/public_html/library/Doctrine/ORM/Mapping/ClassMetadataFactory.php(260): Doctrine\ORM\Mapping\ClassMetadataFactory->newClassMetadataInstance(false)
#3 /Library/Server/Web/Data/Sites/euphoriagaming/public_html/library/Doctrine/ORM/Mapping/ClassMetadataFactory.php(169): Doctrine\ORM\Mapping\ClassMetadataFactory->loadMetadata(false)
#4 /Library/Server/Web/Data/Sites/euphoriagaming/public_html/library/Doctrine/ORM/EntityManager.php(247): Doctrine\ORM\Mapping\ClassMetadataFactory->getMetadataFor(false)
#5 /Library/Server/Web/Data/Sites/euphoriagaming/public_html/library/Doctrine/ORM/UnitOfWork.php(1222): Doctrine\ORM\EntityManager->getClassMetadata(false)
#6 /Library/Server/Web/Data/Sites/euphoriagaming/public_html/library/Doctrine/ORM/UnitOfWork.php(1678): Doctrine\ORM\UnitOfWork->doPersist(1, Array)
#7 /Library/Server/Web/Data/Sites/euphoriagaming/public_html/library/Doctrine/ORM/UnitOfWork.php(1252): Doctrine\ORM\UnitOfWork->cascadePersist(Object(GDS\Entity\Profile), Array)
#8 /Library/Server/Web/Data/Sites/euphoriagaming/public_html/library/Doctrine/ORM/UnitOfWork.php(1201): Doctrine\ORM\UnitOfWork->doPersist(Object(GDS\Entity\Profile), Array)
#9 /Library/Server/Web/Data/Sites/euphoriagaming/public_html/library/Doctrine/ORM/EntityManager.php(454): Doctrine\ORM\UnitOfWork->persist(Object(GDS\Entity\Profile))
#10 /Library/Server/Web/Data/Sites/euphoriagaming/public_html/application/controllers/MemberinfoController.php(81): Doctrine\ORM\EntityManager->persist(Object(GDS\Entity\Profile))
#11 /Library/Server/Web/Data/Sites/euphoriagaming/public_html/library/Zend/Controller/Action.php(516): MemberinfoController->registerAction()
#12 /Library/Server/Web/Data/Sites/euphoriagaming/public_html/library/Zend/Controller/Dispatcher/Standard.php(295): Zend_Controller_Action->dispatch('registerAction')
#13 /Library/Server/Web/Data/Sites/euphoriagaming/public_html/library/Zend/Controller/Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#14 /Library/Server/Web/Data/Sites/euphoriagaming/public_html/library/Zend/Application/Bootstrap/Bootstrap.php(97): Zend_Controller_Front->dispatch()
#15 /Library/Server/Web/Data/Sites/euphoriagaming/public_html/library/Zend/Application.php(366): Zend_Application_Bootstrap_Bootstrap->run()
#16 /Library/Server/Web/Data/Sites/euphoriagaming/public_html/public/index.php(25): Zend_Application->run()
#17 {main}  

控制器内的 registerAction

if ($form->isValid($this->_request->getPost()))
        {   // Valid input
            $username = $this->getRequest()->getParam('username');
            $email = $this->getRequest()->getParam('email');
            $emailconfirm  = $this->getRequest()->getParam('emailconfirm');
            $password = $this->getRequest()->getParam('password');
            $passwordconfirm  = $this->getRequest()->getParam('passwordconfirm');


            $pwdMD5 = md5($password);
            $emailValidateString = md5($username);

            $em = $this->entityManager;

            $account = new GDS\Entity\Account;

            $account->setUsername($username);
            $account->setEmailaddress($email);
            $account->setPassword($pwdMD5);
            $account->setValidationstring($emailValidateString);
            $account->setAccountvalidated(false);

            $em->persist($account);
            $em->flush();

            $accounts = $account = $em->getRepository('GDS\Entity\Account')
                ->findOneBy(array('emailaddress' => $email));

            // Set up initial profile db entry
            $accountProfile = new GDS\Entity\Profile;
            $accountProfile->setAccountId($accounts->getId());

            $em->persist($accountProfile);
            $em->flush();

            $accountProfileDbEntry = $em->getRepository('GDS\Entity\Profile')
                                ->findOneBy(array('AccountId' => $accounts->getId()));

            $ProfileId = $accountProfileDbEntry->getId();

            // Update the Account table entry with the profile Id
            $account->setProfileId($ProfileId);
            $em->persist($account);
            $em->flush();
        }

账户实体

namespace GDS\Entity;
/**
 * @Entity(repositoryClass="GDS\Entity\Repository\AccountRepository")
 */
class Account
{
    public function setAccountvalidated($accountvalidated)
    {
        $this->accountvalidated = $accountvalidated;
    }

    public function getAccountvalidated()
    {
        return $this->accountvalidated;
    }

    public function setEmailaddress($emailaddress)
    {
        $this->emailaddress = $emailaddress;
    }

    public function getEmailaddress()
    {
        return $this->emailaddress;
    }

    public function setPassword($password)
    {
        $this->password = $password;
    }

    public function getPassword()
    {
        return $this->password;
    }

    public function setUsername($username)
    {
        $this->username = $username;
    }

    public function getUsername()
    {
        return $this->username;
    }

    public function setProfileId($profile_id)
    {
        $this->profile_id = $profile_id;
    }

    public function getProfileId()
    {
        return $this->profile_id;
    }

    public function setValidationstring($validationstring)
    {
        $this->validationstring = $validationstring;
    }

    public function getValidationstring()
    {
        return $this->validationstring;
    }

    /**
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

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

    /**
     * @OneToOne(targetEntity="Profile", mappedBy="account", cascade={"persist"})
     */

    private $profile_id;

    /**
     * @Column(type="string", unique=true, length="25", nullable=false)
     */
    private $username;

    /**
     * @Column(type="string", length="25", nullable=true)
     */
    private $password;

    /**
     * @Column(type="string", unique=true, length="75", nullable=false)
     */
    private $emailaddress;

    /**
     * @Column(type="boolean", nullable=false)
     */
    private $accountvalidated;

    /**
    * @Column(type="string", length="125", nullable=true)
    */
    private $validationstring;
}

个人资料实体

namespace GDS\Entity;
/**
 * @Entity(repositoryClass="GDS\Entity\Repository\ProfileRepository")
 */
class Profile
{
    public function setAboutme($aboutme)
    {
        $this->aboutme = $aboutme;
    }

    public function getAboutme()
    {
        return $this->aboutme;
    }

    public function setAccountId($account_id)
    {
        $this->account_id = $account_id;
    }

    public function getAccountId()
    {
        return $this->account_id;
    }

    public function setContactnameBattlelog($contactname_battlelog)
    {
        $this->contactname_battlelog = $contactname_battlelog;
    }

    public function getContactnameBattlelog()
    {
        return $this->contactname_battlelog;
    }

    public function setContactnameGw2($contactname_gw2)
    {
        $this->contactname_gw2 = $contactname_gw2;
    }

    public function getContactnameGw2()
    {
        return $this->contactname_gw2;
    }

    public function setFirstname($firstname)
    {
        $this->firstname = $firstname;
    }

    public function getFirstname()
    {
        return $this->firstname;
    }

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

    /**
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    public function setImageprofile($imageprofile)
    {
        $this->imageprofile = $imageprofile;
    }

    public function getImageprofile()
    {
        return $this->imageprofile;
    }

    public function setLastname($lastname)
    {
        $this->lastname = $lastname;
    }

    public function getLastname()
    {
        return $this->lastname;
    }

    public function setTeamspeakname($teamspeakname)
    {
        $this->teamspeakname = $teamspeakname;
    }

    public function getTeamspeakname()
    {
        return $this->teamspeakname;
    }

    public function setWebsite($website)
    {
        $this->website = $website;
    }

    public function getWebsite()
    {
        return $this->website;
    }

    /**
     * @OneToOne(targetEntity="Account", inversedBy="profile", cascade={"persist"})
     * @JoinColumn(name="account_id", referencedColumnName="id")
     */

    private $account_id;

    /**
     * @Column(type="string", length="25", nullable=true)
     */
    private $firstname;

    /**
     * @Column(type="string", length="25", nullable=true)
     */
    private $lastname;

    /**
     * @Column(type="string", length="25", nullable=true)
     */
    private $imageprofile;

    /**
     * @Column(type="string", length="50", nullable=true)
     */
    private $website;

    /**
     * @Column(type="string", length="50", nullable=true)
     */
    private $teamspeakname;

    /**
     * @Column(type="string", length="50", nullable=true)
     */
    private $contactname_gw2;

    /**
     * @Column(type="string", length="50", nullable=true)
     */
    private $contactname_battlelog;

    /**
     * @Column(type="text", length="500", nullable=true)
     */
    private $aboutme;
}
4

1 回答 1

0
  1. 您的映射不正确。
  2. 你不需要调用persist两次

例如:

class Account
{ 
    /**
    * @OneToOne(targetEntity="Profile", mappedBy="account", cascade={"persist"})
    */
    private $profile;

    public function setProfile(Profile $profile)
    {
        $this->profile = $profile;
        $profile->setAccount($this);
    }
}

class Profile
{
    /**
    * @OneToOne(targetEntity="Account", inversedBy="profile", cascade={"persist"})
    */
    private $account;

    public function setAccount(Account $account)
    {
        $this->account = $account; 
    }
}

进入你的控制器:

$account = new GDS\Entity\Account;
$profile = new GDS\Entity\Profile;

$account->setProfile($profile);
$em->persist($account);
$em->flush();

配置文件实体自动保留(cascade={“persist”})

对不起我的英语不好。

于 2012-10-25T13:54:03.873 回答