Can I access discriminator field from php in doctr

2019-01-08 19:02发布

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.

8条回答
做自己的国王
2楼-- · 2019-01-08 19:09

Use something like this if you want, like me, avoid use of const :

public function getType()
{
    $type = explode('\\', get_class($this));

    return end($type);
}
查看更多
够拽才男人
3楼-- · 2019-01-08 19:12

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

$documentManager->getClassMetadata(get_class($entity))->discriminatorValue;
查看更多
Animai°情兽
4楼-- · 2019-01-08 19:12

Another slicker way than to overload the method in every child, with native symfony :

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

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.

查看更多
再贱就再见
5楼-- · 2019-01-08 19:31

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:

abstract class Attribute {
    const TYPE_BOOL = 0;
    const TYPE_INT  = 1;
    ...
    abstract public function getType();
}

class BooleanAttribute extends Attribute {
    public function getType() {
        return parent::TYPE_BOOL;
    }
}
查看更多
对你真心纯属浪费
6楼-- · 2019-01-08 19:34

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 :

interface AttributeInterface
{
    /**
     * Return the attribute type
     */
    public function getType();
}

Then you create the Attribute abstract class implementing the AttributeInterface interface.

Use the constants in the @DiscrimatorMap call for some consistency

/**
 * Attribute
 * ...
 * @DiscriminatorColumn(name="type", type="string")
 * @DiscriminatorMap({Attribute::TYPE_TEXT = "TextAttribute", Attribute::TYPE_BOOLEAN = "BooleanAttribute", Attribute::TYPE_NUMERIC = "NumericAttribute", Attribute::TYPE_DATE = "DateAttribute"})
 */
abstract class Attribute implements AttributeInterface
{
    const TYPE_TEXT    = 'text';
    const TYPE_BOOLEAN = 'boolean';
    const TYPE_NUMERIC = 'numeric';
    const TYPE_DATE    = 'date';
}

Finally, you create all needed classes, extending Attribute class and implementing the getType() method

/**
 * TextAttribute
 *
 * @ORM\Entity
 */
class TextAttribute extends Attribute
{
    public function getType()
    {
        return $this::TYPE_TEXT;
    }
}

/**
 * BooleanAttribute
 *
 * @ORM\Entity
 */
class BooleanAttribute extends Attribute
{
    public function getType()
    {
        return $this::TYPE_BOOLEAN;
    }
}

/**
 * NumericAttribute
 *
 * @ORM\Entity
 */
class  NumericAttribute extends Attribute
{
    public function getType()
    {
        return $this::TYPE_NUMERIC;
    }
}

/**
 * DateAttribute
 *
 * @ORM\Entity
 */
class DateAttribute extends Attribute
{
    public function getType()
    {
        return $this::TYPE_DATE;
    }
}

// And so on...
查看更多
Luminary・发光体
7楼-- · 2019-01-08 19:34

No that is not possible, but you can do something like: get_class($object) == TYPE_CONST

查看更多
登录 后发表回答