Weird Zero Key Bug In Doctrine Odm

Hi, folks! I’ve spend some time investigating strange Exception thrown by Mongo php client:

MongoException - zero-length keys are not allowed, did you use $ with double quotes?

<div class="awesome">I am awesome div with class</div>

My ODM Entity was:

<?php
/**
 * Class Event
 * @package Webinar\Entity\Event
 *
 * @Document(collection="events")
 * @InheritanceType("SINGLE_COLLECTION")
 * @DiscriminatorField("type")
 * @DiscriminatorMap({"visit"="Visit", "press_button"="PressButton"})
 */
class Event implements EventInterface
{
    /**
     * @var \MongoId
     * @Id
     */
    private $id;

    /**
     * @var int
     * @Field(type="int")
     */
    private $webinarId;

    /**
     * @var int
     * @Field(type="int")
     */
    private $affiliateId;

    /**
     * @var \DateTime
     * @Field(type="date")
     */
    private $dateTime;

    /**
     * @var string
     * @Field(type="string")
     */
    private $pageUrl;

    /**
     * @var Session
     * @ReferenceOne(targetDocument="Webinar\Entity\Session")
     */
    private $session;
}

Actually, bug was hidden in Doctrine ODM documentation. This is how inheritance mapping is described in docs:

<?php
/**
 * @InheritanceType("SINGLE_COLLECTION")
 * @DiscriminatorField("type")
 * @DiscriminatorMap({"visit"="Visit", "press_button"="PressButton"})
 */

And what you REALLY should write is:

<?php
/**
 * @InheritanceType("SINGLE_COLLECTION")
 * @DiscriminatorField(fieldName="type")
 * @DiscriminatorMap({"visit"="Visit", "press_button"="PressButton"})
 */

And that’s it!

Alex Panshin avatar
About Alex Panshin
Software Engineer from Russia. Interested in PHP, Scala, microservices and Big/Fast Data stuff.
comments powered by HyperComments