Oto, jak to zrobię.
pierwsze, dokonał AttributeInterface
, aby mieć pewność, że w przyszłości wszystkie nowe typy atrybut będzie wdrożyć metody potrzebne:
interface AttributeInterface
{
/**
* Return the attribute type
*/
public function getType();
}
Następnie należy utworzyć Attribute
klasa abstrakcyjna wykonawczych interfejsu AttributeInterface
.
użyć stałych w zaproszeniu @DiscrimatorMap
jakiegoś konsystencji
/**
* 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';
}
Wreszcie, należy utworzyć wszystkie potrzebne lekcje, rozszerzając Attribute
klasę i wdrożenie metody getType()
/**
* 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...
Możliwy duplikat [Mapuj kolumnę dyskryminatora do pola z Doctrine 2] (https://stackoverflow.com/questions/21284964/map-a-discriminator-column-to-a-field-with-doctrine-2) – sroes