Can I access discriminator field from php in doctrine2?

后端 未结 8 727
遇见更好的自我
遇见更好的自我 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:01

    My approach is to simply access it's value through the meta data doctrine generates

    $cmf = $em->getMetadataFactory();
    $meta = $cmf->getMetadataFor($class);
    $meta->discriminatorValue
    

    will give you the value, so as a method

    public static function get_type()
    {
        //...get the $em instance 
        $cmf = $em->getMetadataFactory();
        $meta = $cmf->getMetadataFor(__CLASS__);
        return $meta->discriminatorValue;
    }
    

    I cache the metadata in a static variable for each class that extends my base entity, there is a lot of other useful information in there as well ...

提交回复
热议问题