Doctrine2 Merge()因复合索引失败| Doctrine2 Bug?

时间:2013-12-15 06:11:06

标签: php symfony doctrine-orm

当我合并使用复合索引的以下分离的 UserAnswer 对象时,当且仅当我的索引具有两者外键/映射实体时,合并才会失败< em>和对象中的非外键/映射实体。如果复合索引仅包含外键或仅包含非外键,则合并将起作用。

我认为这是一个Doctrine2错误。如果没有,我做错了什么?

Doctrine2版本:2.3.4

以下控制器代码保存 UserAnswer 对象类型,在以下代码块中对此进行了描述:

$entityManager = $this->getDoctrine()->getManager();

$userAnswer = new UserAnswer();
$userAnswer->setUser($this->getUser());
$userAnswer->setAnswerType(1);
$userAnswer->setQuestion($entityManager->getReference('AcmeBundle:Question',1));

$entityManager->detach($userAnswer);
$entityManager->merge($userAnswer);
try {
    $entityManager->flush();
} catch (\Doctrine\DBAL\DBALException $e) {
...

UserAnswer 对象:

/**
 * @ORM\Entity
 * @ORM\Table(name="userAnswers")
 */
class UserAnswer
{
    /**
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="User")
     */
    protected $user;

    /**
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="Question")
     */
    protected $question;

    /**
     * @ORM\Id
     * @ORM\Column(name="answer_type", type="integer")
     */
    protected $answer_type;
...

这是我收到的错误:

  

查询的标识符answer_type缺失   AcmeBundle \实体\ UserAnswer

     

500内部服务器错误 - ORMException

     

堆栈跟踪

     

在   /vagrant/Symfony/vendor/doctrine/orm/lib/Doctrine/ORM/ORMException.php   在第160行 -

public static function missingIdentifierField($className, $fieldName)
{
    return new self("The identifier $fieldName is missing for a query of " . $className);
}

public static function overwriteInternalDQLFunctionNotAllowed($functionName)
     

at ORMException :: missingIdentifierField('Acme \ Entity \ UserAnswer','answer_type')in /vagrant/Symfony/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php at 380 +

     

在EntityManager - &gt;在/ vagrant / Symfony / vendor / doctrine中查找('Acme \ Entity \ UserAnswer',数组('用户'=&gt;'1','问题'=&gt;'1')) /orm/lib/Doctrine/ORM/UnitOfWork.php第1787行+

     

在UnitOfWork - &gt; doMerge(对象(UserAnswer),数组('0000000025cf9503000000006ed40a0a'=&gt;对象(UserAnswer)))/vagrant/Symfony/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php在第1701行+

     

在UnitOfWork - &gt;合并(对象(UserAnswer))在/vagrant/Symfony/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php第638行+

     

at EntityManager - &gt; merge(object(UserAnswer))在/vagrant/Symfony/src/Acme/Controller/UserAnswerController.php第23行

     

at UserAnswerController - &gt; postAction()at call_user_func_array(array(object(UserAnswerController),'postAction'),array())in kernel.root_dir / bootstrap.php.cache at line 2815 +

     

at HttpKernel - &gt; handleRaw(object(Request),'1')in kernel.root_dir / bootstrap.php.cache at line 2789 +

     

at HttpKernel-&gt; handle(object(Request),'1',true)in kernel.root_dir / bootstrap.php.cache at 2918 +

     

在ContainerAwareHttpKernel - &gt; handle(object(Request),'1',true)在kernel.root_dir / bootstrap.php.cache第2220行

     

at Kernel-&gt; handle(object(Request))invagrant/Symfony/web/app_dev.php at line 31 +

UserAnswer 使用上述控制器代码产生错误:

/**
 * @ORM\Entity
 * @ORM\Table(name="userAnswers")
 */
class UserAnswer
{
    /**
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="User")
     */
    protected $user;

    /**
     * @ORM\Id
     * @ORM\ManyToOne(targetEntity="Question")
     */
    protected $question;

    /**
     * @ORM\Column(name="answer_type", type="integer")
     */
    protected $answer_type;
...

这也不是:

/**
 * @ORM\Entity
 * @ORM\Table(name="userAnswers")
 */
class UserAnswer extends ApiObject
{
    /**
     * @ORM\Id
     * @ORM\Column(name="userid", type="integer")
     */
    protected $userid;

    /**
     * @ORM\Id
     * @ORM\Column(name="question_id", type="integer")
     */
    protected $question_id;

    /**
     * @ORM\Id
     * @ORM\Column(name="answer_type", type="integer")
     */
    protected $answer_type;
...

2 个答案:

答案 0 :(得分:1)

这个 一个Doctrine2错误,并在Doctrine版本2.4.2中修复,在撰写本文时尚未发布。 (见http://www.doctrine-project.org/jira/browse/DDC-2645。)

但是,此修复程序 非常 易于实施:https://github.com/Exeu/doctrine2/commit/edab2b6a9670dda3127f457205c7720611d6a6c9

答案 1 :(得分:0)

看起来这可能与已知的Doctrine问题有关,并在版本2.3.4中修复:

http://www.doctrine-project.org/jira/browse/DDC-2645