Weird Doctrine ODM exception when using references together with inheritance

痴心易碎 提交于 2019-12-22 11:23:23

问题


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.

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:

Notice: Undefined index: in C:\xampp\htdocs\Symfony\vendor\doctrine\mongodb-odm\lib\Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo.php line 1293

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

Here are my classes

File-Class:

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:

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


}

Game-Class:

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
    }

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.


回答1:


I think it is too late for your question, but maybe there are other users having the same problem (as me).

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.

Solution is to not reference the abstract class Foobar (=MappedSuperclass) but a concrete implementation (=Document) - as in your case - Game.

See also Doctrine ODM returns proxy object for base class instead of sub-classed document



来源:https://stackoverflow.com/questions/23496987/weird-doctrine-odm-exception-when-using-references-together-with-inheritance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!