Doctrine2协会持久性

时间:2013-09-20 11:22:11

标签: php doctrine-orm zend-framework2

过去一周左右,我一直在玩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为对象,在第1588行(路径)\ htdocs \ vendor \ doctrine \ orm \ lib \ Doctrine \ ORM \ UnitOfWork.php中给出整数

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

1 个答案:

答案 0 :(得分:0)

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

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

  • 没有名为DeveloperUser的课程

以及

  • 你确定在数据库中,所有user_type只有1或2吗? (不为空,不为0等)