[英]insert with a calculated field in doctrine
I want to make an insert with a calculated field in doctrine. 我想插入一个带有理论值的计算字段。 If I would do it with SQL it would look like:
如果我要用SQL做到这一点,它将看起来像:
INSERT INTO "TableX"
( "ID_Special",
"Name"
)
VALUES ( (select MAX(ID_Special) + 1 from "TableX"),
"blabla"
)
"ID_Special" should be an autoinc-Value without beeing unique. “ ID_Special”应为autoinc-Value,且不能唯一。 So there are other INSERT's made on "TableX" that insert an already existing "ID_Special".
因此,在“ TableX”上进行了其他插入,插入了已经存在的“ ID_Special”。
Now I am going the following way: 现在,我将按照以下方式进行操作:
$qb = $em->createQueryBuilder();
$qb->select('MAX(r.ID_Special)')->from('TableX', 'r');
$nextIDSpecial = $qb->getQuery()->getSingleScalarResult();
...
$dataset = new TableX();
$dataset->setIDSpecial($nextIDSpecial);
$dataset->setName('blabla');
$em->persist($dataset);
$em->flush();
But this might run into problems, if I have multiuser access. 但是,如果我具有多用户访问权限,则可能会遇到问题。
Is there a way to create the first shown SQL-statement with DQL? 有没有一种方法可以使用DQL创建第一个显示的SQL语句?
Or is there a way to make my second approach atomar? 还是有办法使我的第二种方法变得原子化?
The best way I found until now: I created a table named "IDSpecialGenerator" with only one column of type integer, pk, not null and autoinc. 到目前为止,我发现的最好方法是:创建了一个名为“ IDSpecialGenerator”的表,其中只有一列类型为整数,pk,不为null和autoinc。 Everytime I want to insert a dataset into "TableX" with a new "ID_Special" I first have to make an insert into "IDSpecialGenerator".
每次我想使用新的“ ID_Special”将数据集插入“ TableX”时,我首先都必须在“ IDSpecialGenerator”中进行插入。 So my database makes the calculation, I have no problems with multiuseraccess and I not have to use native SQL.
因此,我的数据库进行了计算,我对多用户访问没有任何问题,也不必使用本机SQL。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.