I have an entity which defines inheritance like this:
* @DiscriminatorColumn(name="type", type="string")
* @DiscriminatorMap({"text" = "TextAttribute", "boolean" = "BooleanAttribute", "numeric" = "NumericAttribute", "date" = "DateAttribute"})
I am wondering is it possible to have getter for field 'type'? I know I can use instanceof (and in most cases this is what I'm doing) but there are few scenarios where $item->getType() would make my life so much easier.
Use something like this if you want, like me, avoid use of const :
It's possible either with the EntityManager or using the DocumentManager.
Another slicker way than to overload the method in every child, with native symfony :
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.
Extending what beberlei said, you could declare some constants in the Attribute class, and an abstract
getType()
function. Then, overload it in every derived attribute class.Something like:
Here is how I'd do.
First, you made an
AttributeInterface
, to be sure that all future new Attribute types will implement the need method :Then you create the
Attribute
abstract class implementing theAttributeInterface
interface.Use the constants in the
@DiscrimatorMap
call for some consistencyFinally, you create all needed classes, extending
Attribute
class and implementing thegetType()
methodNo that is not possible, but you can do something like: get_class($object) == TYPE_CONST