对带有必需字段的Doctrine类表继承进行查询

问题描述 投票:0回答:1

我的网域有一个父类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,这里的问题是子类具有必填字段,但这不应该是塞子,对吧?

symfony doctrine discriminator class-table-inheritance
1个回答
0
投票

我发现了可能的解决方法。在我的自定义DBAL类型FileIdType上,我在实例化FileId之前检查了该值是否为空:

use Doctrine\DBAL\Types\Type;

class FileIdType extends Type
{
    public function convertToPHPValue($value, AbstractPlatform $platform)
    {
        return $value ? new FileId($value) : null;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.