Can I access discriminator field from php in doctrine2?

后端 未结 8 699
遇见更好的自我
遇见更好的自我 2020-12-01 04:44

I have an entity which defines inheritance like this:

* @DiscriminatorColumn(name=\"type\", type=\"string\")
* @DiscriminatorMap({\"text\" = \"TextAttribute\         


        
相关标签:
8条回答
  • 2020-12-01 05:15

    Another slicker and faster way than to overload the method in every child or define a constant in every child is to use reflection class to retrieve the name of the class without the namespace :

    public function getType() {
        return (new \ReflectionClass($this))->getShortName();
    }
    

    It also works in any php version since php 5

    It might not return exactly the discriminator name depending on your discriminator map declaration but it will return the child entity name (the class name) which is a great way to name and distinguish the different subentities

    Without a need to define anything in the subclasses.

    0 讨论(0)
  • 2020-12-01 05:18

    It's possible either with the EntityManager or using the DocumentManager.

    $documentManager->getClassMetadata(get_class($entity))->discriminatorValue;
    
    0 讨论(0)
提交回复
热议问题