将引用与继承一起使用时出现奇怪的Doctrine ODM异常 [英] Weird Doctrine ODM exception when using references together with inheritance
问题描述
我有三节课。 File-Class引用了Foobar,Game继承自Foobar。还有一些其他类也从Foobar继承,但我把它们排除在外,因为它们在这里不相关。我还省略了一些不相关的领域和他们的吸气剂和制定者。
I've got three classes. The File-Class has a reference to Foobar and Game inherits from Foobar. There are some other Classes which also inherit from Foobar but i left them out as they aren't relevant here. I also left out some unrelevant fields and their getters and setters.
计划是每个游戏都有两个图像,mainImage和secondaryImage。我把这些字段放到了一个单独的类中,游戏继承了这个类,因为我也需要它们用于其他几个类。
The plan is that every Game has two images, the mainImage and the secondaryImage. I've put those fields into a seperate class from which Game inherits because i need them for a few other classes too.
我的问题是,如果我加载游戏数据库一旦我尝试迭代它们我得到以下异常:
My problem is that if I load the games from the database as soon as i try to iterate over them I get the following exception:
注意:未定义的索引:在C:\ xampp中\htdocs\Symfony\vendor\doctrine\mongodb-odm\lib\Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo.php线1293
Notice: Undefined index: in C:\xampp\htdocs\Symfony\vendor\doctrine\mongodb-odm\lib\Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo.php line 1293
有关参考这里是ClassMetadataInfo.php的线
For reference here are the lines of ClassMetadataInfo.php
public function getPHPIdentifierValue($id)
{
$idType = $this->fieldMappings[$this->identifier]['type'];
return Type::getType($idType)->convertToPHPValue($id);
}
这是我的课程
文件类:
namespace Project\MainBundle\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
/**
* @MongoDB\Document
*/
class File
{
/**
* @MongoDB\Id(strategy="INCREMENT")
*/
protected $id;
/**
* @MongoDB\ReferenceOne(targetDocument="Foobar", inversedBy="mainImage")
*/
private $mainImage;
/**
* @MongoDB\ReferenceOne(targetDocument="Foobar", inversedBy="secondaryImage")
*/
private $secondaryImage;
/**
* Get id
*/
public function getId()
{
return $this->id;
}
public function setMainImage($mainImage)
{
$this->mainImage = $mainImage;
return $this;
}
public function getMainImage()
{
return $this->mainImage;
}
public function setSecondaryImage($secondaryImage)
{
$this->secondaryImage = $secondaryImage;
return $this;
}
public function getSecondaryImage()
{
return $this->secondaryImage;
}
}
Foobar-Class :
Foobar-Class:
namespace Project\MainBundle\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
/**
* @MongoDB\MappedSuperclass
*/
abstract class Foobar
{
/**
* @MongoDB\Id(strategy="INCREMENT")
*/
protected $id;
/**
* @MongoDB\ReferenceOne(targetDocument="File", mappedBy="mainImage")
*/
protected $mainImage;
/**
* @MongoDB\ReferenceOne(targetDocument="File", mappedBy="secondaryImage")
*/
protected $secondaryImage;
/**
* Get id
*/
public function getId()
{
return $this->id;
}
/**
* Set mainImage
*/
public function setMainImage($file)
{
$file->setMainImage($this);
$this->mainImage = $file;
return $this;
}
/**
* Get mainImage
*/
public function getMainImage()
{
return $this->mainImage;
}
/**
* Set secondaryImage
*/
public function setSecondaryImage($file)
{
$file->setSecondaryImage($this);
$this->secondaryImage = $file;
return $this;
}
/**
* Get secondaryImage
*/
public function getSecondaryImage()
{
return $this->secondaryImage;
}
}
游戏级:
namespace Project\MainBundle\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
/**
* @MongoDB\Document
*/
class Game extends Foobar
{
/**
* @MongoDB\String
*/
private $name;
/**
* Set name
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*/
public function getName()
{
return $this->name;
}
}
虽然没有真的很重要,但这是我要执行的功能:
Though it doesn't really matter but here is my function i want to execute:
$dm = $this->get('doctrine_mongodb')->getManager();
$games_all = $dm->getRepository("ProjectMainBundle:Game")->createQueryBuilder()->sort('id', 'ASC')->getQuery()->execute();
foreach ($games_all as $singlegame) { // it breaks here
// Here i would do stuff
}
这是Doctrine ODM中的错误还是我做错了什么?这些课程是否正确?我已经尝试了一切,但它不会工作。
Is this a bug in Doctrine ODM or am I doing something wrong? Are the classes correct? I have tried everything but it just wont work.
推荐答案
我认为你的问题为时已晚,但也许还有其他问题具有相同问题的用户(和我一样)。
I think it is too late for your question, but maybe there are other users having the same problem (as me).
问题与Foobar是MappedSuperclass有关。如在 https://github.com/doctrine/由你描述有同样的问题mongodb-odm / issues / 241 。
The problem is related to Foobar being a MappedSuperclass. Had the same problem as described by you and at https://github.com/doctrine/mongodb-odm/issues/241.
解决方案是不引用抽象类Foobar(= MappedSuperclass),而是引用具体实现(= Document) - as在你的情况下 - 游戏。
Solution is to not reference the abstract class Foobar (=MappedSuperclass) but a concrete implementation (=Document) - as in your case - Game.
另见 Doctrine ODM返回基类的代理对象而不是子类文档
这篇关于将引用与继承一起使用时出现奇怪的Doctrine ODM异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!