Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Single Collection Inheritance mapping does NOT work if the subclasses are in a different namespace #124

Closed
epicwhale opened this Issue Jul 9, 2011 · 3 comments

Comments

Projects
None yet
3 participants
Contributor

epicwhale commented Jul 9, 2011

Problem Description

  • I have a parent class Exchange and a subclass PersonalMessage in two different namespaces.
  • I create an instance of the PersonalMessage class and persist it using the document manager.
  • The document is successfully persisted, BUT _the discriminator field is NOT SET_ in the database. (bug)
  • As the discriminator field is not set, while finding the document and returning, obviously, the instance created does not belong to the subclass but of the parent class instead. (In my case, the parent class is abstract, but that doesn't make a difference.)
  • If I move BOTH the classes into the SAME NAMESPACE (be it any), the 'type' field is set while persisting the document. Everything works fine. This definitely means it's an issue with them being in separate namespaces.

Additional Info (may not be useful)

  • If I go directly into the database and try to retrieve a document of the parent type, it perfectly resolves the discriminator field and returns the document of the appropriate subclass instead.

My Example Code

Here's my example parent document and inherited document in two different namespaces:

_My Parent Class in the '\Document' namespace_

<?php
namespace Document;

/**
 *
 * @abstract
 * @Document
 * @InheritanceType("SINGLE_COLLECTION")
 * @DiscriminatorField(fieldName="type")
 * @DiscriminatorMap({"p"="\Document\Exchange\PersonalMessage","s"="\Document\Exchange\Share"})
 */
abstract class Exchange
{
    /**
     * @Id
     */
    protected $id;

    /**
     * Time of exchange
     *
     * @var \Datetime
     * @Date
     */
    protected $time;

    public function setTime($time)
    {
        $this->time = $time;
    }
}

_Example subclass in the '\Document\Exchange' namespace_

<?php
namespace Document\Exchange;

/**
 * Document for Personal Messages (extended from Exchange document)
 *
 * @Document
 */
class PersonalMessage extends \Document\Exchange
{
    /**
     * @var String
     * @String
     */
    protected $foo;


    public function setFoo($foo)
    {
        $this->foo = $foo;
    }
}
Member

avalanche123 commented Jul 9, 2011

need to make clear that class names should be used like "Foo\Bar" and not "\Foo\Bar" or tweak code to support both

Contributor

epicwhale commented Jul 10, 2011

I think you should tweak the code to support both. Cause "\Foo\Bar" is also legit. In fact, if you are defining an @return or @var in PHPDoc, then you must use the \ prefix else it does not work in certain IDE's.

Owner

jwage commented Feb 7, 2012

That is not true actually. In PHP when you specify the FQN as a string, it does not include the \

@jwage jwage closed this Feb 7, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment