我的网域有一个父类IncidenceMessage
和几个子类(即IncidenceMessageText
)。我有以下类表继承配置:
Domain\Model\IncidenceMessage\IncidenceMessage:
type: entity
repositoryClass: Infrastructure\Domain\Model\IncidenceMessage\DoctrineIncidenceMessageRepository
table: incidence_messages
inheritanceType: JOINED
discriminatorColumn:
name: type
type: string
length: 30
discriminatorMap:
text: IncidenceMessageText
image: IncidenceMessageImage
audio: IncidenceMessageAudio
video: IncidenceMessageVideo
fields:
...
我可以正确创建任何IncidenceMessage
实体。
在数据库中只有IncidenceMessageText
,当我尝试获取事件消息时,出现以下错误:
TypeError: Argument 1 passed to Domain\Model\File\FileId::__construct() must be of the type string, null given
([FileId
是代表文件实体ID的值对象]
[IncidenceMessageImage
具有作为外键的File
字段,它是必需的。
[当File
没有该字段时,Doctrine提取IncidenceMessageText
对我来说毫无意义。
[在调试时,我发现主义对每个IncidenceMessage
表使用LEFT JOINs进行了SELECT,这调用了我的FileTypeId::convertToPHPValue
方法:
class FileIdType extends Type
{
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
return new FileId($value);
}
}
AFAIK,这里的问题是子类具有必填字段,但这不应该是塞子,对吧?
我发现了可能的解决方法。在我的自定义DBAL类型FileIdType
上,我在实例化FileId
之前检查了该值是否为空:
use Doctrine\DBAL\Types\Type;
class FileIdType extends Type
{
public function convertToPHPValue($value, AbstractPlatform $platform)
{
return $value ? new FileId($value) : null;
}
}