【发布时间】:2020-06-04 00:37:21
【问题描述】:
如何在 Doctrine 2 中对非主键列使用串行数据类型?
Doctrine 可以使用串行列作为主键,生成器策略设置为 SEQUENCE 或 IDENTITY,但生成器不能用于非主键列。
我有这样的表:
create table test (
id integer not null generated by default as identity,
order serial
)
constraint test_pk primary key (id)
教义实体:
App\Entity\Test:
type: entity
table: test
id:
id:
type: integer
nullable: false
id: true
generator:
strategy: SEQUENCE
sequenceGenerator:
sequenceName: test_id_seq
fields:
order:
type: integer
nullable: false
column: '"order"'
我想要的是,当我在这个表中插入新行时,我不必明确指定顺序,但 db 应该处理它。
原则上生成的 sql 代码应如下所示:
insert into test (DEFAULT, DEFAULT);
在没有任何配置的情况下,学说尝试在“order”列中插入null,导致错误“can't insert null value into not null field”
我尝试了什么
我尝试在 php 实体类中为“order”属性设置默认值,如下所示:
private $order = 'DEFAULT'
但这错误地显示为“整数的无效语法”
然后我尝试将选项放入 yaml 定义中,如下所示:
fields:
order:
type: integer
nullable: false
column: '"order"'
options:
default: DEFAULT
这并没有什么不同。
比我尝试创建名为 order 的自定义类型,但也失败了。
自定义类型代码:
use Doctrine\DBAL\Types\Type;
class OrderType extends Type
{
public function getName(): string
{
return 'order';
}
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
if (is_numeric($value)) {
return $value;
}
return 'DEFAULT';
}
public function convertToDatabaseValueSQL($sqlExpr, AbstractPlatform $platform)
{
return 'DEFAULT';
}
public function canRequireSQLConversion(): bool
{
return true;
}
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
return 'serial';
}
}
这以错误“SQLSTATE[HY093]: Invalid parameter number: parameter was not defined”结束,这是方法 convertToDatabaseValueSQL 的问题。当我将其注释掉并使用默认实现时,错误“SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for integer: "DEFAULT"”
它仍然引用单词 DEFAULT 但我需要它不被引用,它必须用作关键字而不是字符串。
我认为自定义类型应该很有效。在旧的 Zend Framework v1 中,有一个 Zend_Db_Exp 类,当在 sql 语句中使用时,它没有被引用,并且值按原样使用。我想我在这里需要类似的东西。
版本信息
PHP 7.3
Postgres 11
Doctrine ORM 2.6(如果需要我会升级)
【问题讨论】:
标签: php postgresql doctrine-orm