将引用与继承一起使用时出现奇怪的Doctrine ODM异常

时间:2014-05-06 14:04:52

标签: symfony inheritance doctrine-odm

我有三节课。 File-Class引用Foobar和Game继承自Foobar。还有一些其他类也从Foobar继承,但我把它们排除在外,因为它们在这里并不相关。我还遗漏了一些不相关的领域和他们的吸气者和制定者。

计划是每个游戏都有两个图像,即mainImage和secondaryImage。我把这些字段放到了一个单独的类中,游戏继承了这个类,因为我也需要它们用于其他一些类。

我的问题是,如果我在尝试迭代它们时立即从数据库加载游戏,我会得到以下异常:

  

注意:未定义的索引:在C:\ xampp \ htdocs \ Symfony \ vendor \ doctrine \ mongodb-odm \ lib \ Doctrine \ ODM \ MongoDB \ Mapping \ ClassMetadataInfo.php第1293行

以下是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的级:

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;
    }


}

虽然它并不重要,但这是我想要执行的功能:

    $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中的错误还是我做错了什么?这些课程是否正确?我已经尝试了一切,但它不会工作。

1 个答案:

答案 0 :(得分:2)

我认为你的问题为时已晚,但也许还有其他用户遇到同样的问题(和我一样)。

问题与Foobar是MappedSuperclass有关。遇到了您和https://github.com/doctrine/mongodb-odm/issues/241所描述的相同问题。

解决方案是不引用抽象类Foobar(= MappedSuperclass),而是引用具体实现(= Document) - 就像你的情况一样 - Game。

另见Doctrine ODM returns proxy object for base class instead of sub-classed document