Doctrine2 关联持久性

Posted

技术标签:

【中文标题】Doctrine2 关联持久性【英文标题】:Doctrine2 Association Persistence 【发布时间】:2013-09-20 11:22:11 【问题描述】:

在过去一周左右的时间里,我一直在玩弄 Zend 和 Doctrine。我已经掌握了基本插入和选择的窍门,并且还可以使用 DQL 从连接的表中进行选择。我遇到的问题是保留关联实体。我得到的错误是:(path)htdocs\vendor\doctrine\common\lib\Doctrine\Common\Persistence\Mapping\MappingException.php:96 with the message 'Class "" does not exist'.

我的代码在下面...

这是主要实体(“多”端的那个)

namespace Project\Entity;

use Doctrine\ORM\Mapping as ORM;
use Zend\InputFilter\Factory as InputFactory;

/**
 * ClientUser
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class ClientUser extends SystemUser

/**
 * @var integer
 * 
 * @ORM\OneToOne(targetEntity="SystemUser", cascade="persist", "remove")
 * @ORM\JoinColumn(name="id", referencedColumnName="id")
 */
private $id;

/**
 * @var integer
 *
 * @ORM\ManyToOne(targetEntity="Client", cascade="persist", "remove")
 * @ORM\JoinColumn(name="client", referencedColumnName="id")
 */
private $client;

protected $_inputFilter;

//Other stuff here...

这里是“客户端”关联实体...

namespace Project\Entity;

use Doctrine\ORM\Mapping as ORM;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface; 

/**
 * Client
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class Client implements InputFilterAwareInterface

/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="client_name", type="string", length=255)
 */
private $clientName;

/**
 * @var integer
 *
 * @ORM\Column(name="loggable_hours", type="integer")
 */
private $loggableHours;

/**
 * @var float
 * 
 * @ORM\Column(name="normal_rate", type="decimal", scale=2)
 */
private $normalRate;

/**
 * @var float
 * 
 * @ORM\Column(name="critical_rate", type="decimal", scale=2)
 */
private $criticalRate;

/**
 * @var string
 *
 * @ORM\Column(name="start_date", type="string")
 */
private $startDate;

/**
 * @var boolean
 *
 * @ORM\Column(name="enabled", type="boolean")
 */
private $enabled;

/**
 * @var integer
 *
 * @ORM\Column(name="critical_hours", type="integer")
 */
private $criticalHours;

/**
 *
 * @var type 
 */
protected $_inputFilter;

//Other stuff (getters,setters, etc)

ClientUser 与以下对象具有一对一的关系:

namespace Project\Entity;

use Doctrine\ORM\Mapping as ORM;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface; 

/**
 * SystemUser
 *
 * @ORM\Table()
 * @ORM\Entity
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="user_type", type="integer")
 * @ORM\DiscriminatorMap(1 = "DeveloperUser", 2 = "ClientUser")
 * 
 */
class SystemUser implements InputFilterAwareInterface 
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="username", type="string", length=100, unique=true)
 */
private $username;

/**
 * @var string
 *
 * @ORM\Column(name="password", type="string", length=255)
 */
private $password;

/**
 * @var string
 *
 * @ORM\Column(name="user_first_name", type="string", length=255)
 */
private $userFirstName;

/**
 * @var string
 *
 * @ORM\Column(name="user_surname", type="string", length=255)
 */
private $userSurname;

/**
 * @ORM\Column(type="string", length=32)
 */
private $salt;

/**
 * @var \DateTime
 *
 * @ORM\Column(name="last_login", type="datetime")
 */
private $lastLogin = '0000-00-00 00:00:00';

/**
 * @var bool
 * 
 * @ORM\Column(name="enabled", type="boolean", options="default" = 1)
 */
private $enabled = 1;

/**
 * For the input filter...
 * 
 * @var InputFilter
 */
protected $_inputFilter;

//The rest...

我完全不知道这里可能出了什么问题...为了完整起见,这里是控制器“添加”操作...

public function addAction() 
    //To add clients
    $form = new ClientUserForm($this->getServiceLocator()
                                     ->get('Doctrine\ORM\EntityManager'));
    $form->get('submit')->setValue('Add');

    $request = $this->getRequest();
    if ($request->isPost()) 
        $clientUser = new ClientUser();
        $form->setInputFilter($clientUser->getInputFilter());
        $form->setData($request->getPost());

        if ($form->isValid()) 
            $clientUser->populate($form->getData());
            /**
            *This bottom line is where I get the exception!
            */
            $this->getEntityManager()->persist($clientUser);
            $this->getEntityManager()->flush();

            //Redirect
            return $this->redirect()->toRoute('client_user');
        
    

    return array ('form' => $form);

任何帮助都会很棒!如果我只知道应该是哪个班级“”,我可能会比现在更好! 谢谢女士们先生们,你们摇滚!

编辑-

忘记添加这 2 个 PHP 警告...

警告:spl_object_hash() 期望参数 1 为对象,整数在 (path)\htdocs\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php 第 1588 行中给出

警告:get_class() 期望参数 1 为对象,整数在 (path)\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php 第 1596 行中给出

【问题讨论】:

【参考方案1】:

我不确定会发生什么,但我发现了一些我不理解的东西。当您声明继承时,您使用:

 * @ORM\DiscriminatorColumn(name="user_type", type="integer")
 * @ORM\DiscriminatorMap(1 = "DeveloperUser", 2 = "ClientUser")

但是

没有一个名为 DeveloperUser 的类

还有

您确定在数据库中,所有 user_type 都只是 1 或 2? (不为空,不为 0 等)

【讨论】:

以上是关于Doctrine2 关联持久性的主要内容,如果未能解决你的问题,请参考以下文章

从doctrine2中的代理对象获取“true”对象

添加 Spring 事务时三重关联的持久性

java 供应商实体使用JPA注释创建一个可以与项目关联+将其持久性级联到项目,以便它们可以成为sav

[Doctrine2] 刷新失败但 unitOfWork 进程正确

持久层框架--hibernate

休眠持久化实体而不获取关联对象。仅凭身份证