这是我的实体:
class Foo
{
/**
* @var int
* @ORM\Id
* @ORM\Column(name="custom_id", type="integer")
* @ORM\GeneratedValue(strategy="NONE")
*/
private $customId;
public function __construct( $customId )
{
$this->customId = $customId;
}
}
现在我正在生成一个新实例
$foo = new Foo(123);
dump($foo); // shows customId: 123
$em->persist($foo);
$em->flush();
$em->clear();
An exception occurred while executing 'INSERT INTO table_foo (custom_id, foo, bar) VALUES (?, ?, ?)' with params [null, null, null]:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'custom_id' cannot be null
我知道它不能为 NULL,这就是我设置它的原因。 为什么 doctrine 会用 NULL 覆盖值?策略是 NONE 并且发生在构造函数中......我在这里错过了什么?
最佳答案
有点晚了,但如果有人会搜索它,你需要使用自定义值生成器(什么都不做,只是将你的插入语句)
/**
* @var int
* @ORM\Id
* @ORM\Column(name="custom_id", type="integer")
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class="Doctrine\ORM\Id\AssignedGenerator")
*/
private $customId;
关于php - Doctrine GeneratedValue=NONE 不断为 Id 插入 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41106052/