php - Doctrine 只在 foreach 循环后插入一条记录
问题描述
我有一个简单的数组,其中包含一些需要插入数据库的值。在所有值中,仅实际插入最后一个值。
当我将flush();
循环放入内部时,记录会被插入。
当我在刷新之前(在 foreach 之外)转储实体管理器时,我确实看到了对所有值(实体)的引用。
虽然,只插入最后一条记录。它确实得到了 id #3,所以其他的似乎在某个地方丢失了。
$values = [
"val1", "val2", "val3"
];
foreach ($values as $value) {
$i = new MyEntityClass();
$i->setVerified(false);
$i->setName($value);
$this->em->persist($i);
}
$this->em->flush();
更新:我连接了一个事件监听器来进行刷新前和刷新后。
public function preFlush(PreFlushEventArgs $args) {
$em = $args->getEntityManager();
foreach ($em->getUnitOfWork()->getScheduledEntityInsertions() as $entity) {
dump($entity->getName());
}
}
public function postFlush(PostFlushEventArgs $args) {
dd($args->getEntityManager()->getUnitOfWork()->getScheduledEntityInsertions());
}
在 preFlush 中,所有值都清楚地打印出来,而 postFlush 转储为空。
更新 2:我正在使用 uuid_binary_ordered_time 如下
/**
* @ORM\Id
* @ORM\Column(type="uuid_binary_ordered_time", unique=true)
* @GeneratedValue(strategy="CUSTOM")
* @CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidOrderedTimeGenerator")
* @Groups({"uuid"})
*/
protected $id;
使用10.4.8-MariaDB
和 php v7.3.10
更新 3: 我仍在尝试解决这个问题,现在有一个不同的场景,但我仍然遇到多个操作未执行的问题。这可能与数据库配置相关而不是 Doctrine 吗?
案例:从A表和B表中收集数据,向C表插入数据,从A表中删除记录。
通过遵循@Jakumi 的建议(或查看日志),我可以看到正在执行以下查询(剧透,表 A 中的行被删除,但表 C 中没有新行):
"queries": {
"1": {
"sql": "SELECT t0.email AS email_1, t0.status AS status_2, t0.payment_provider_id AS payment_provider_id_3, t0.payment_method AS payment_method_4, t0.quantity AS quantity_5, t0.price_total AS price_total_6, t0.created_on AS created_on_7, t0.updated_on AS updated_on_8, t0.paid_on AS paid_on_9, t0.id AS id_10, t0.s_uuid AS s_uuid_11, t0.product_id AS product_id_12 FROM payment_pending t0 WHERE t0.payment_provider_id = ? LIMIT 1",
"params": [
"tr_S432rV6fhM"
],
"types": [
"string"
],
"executionMS": 0.0005559921264648438
},
"2": {
"sql": "SELECT t0.username AS username_1, t0.roles AS roles_2, t0.password AS password_3, t0.email AS email_4, t0.email_verified AS email_verified_5, t0.created_on AS created_on_6, t0.registration_method AS registration_method_7, t0.has_premium AS has_premium_8, t0.premium_until AS premium_until_9, t0.verified_mobile AS verified_mobile_10, t0.active AS active_11, t0.facebook_id AS facebook_id_12, t0.google_id AS google_id_13, t0.id AS id_14, t0.s_uuid AS s_uuid_15, t16.is_a AS is_a_17, t16.wants_a AS wants_a_18, t16.firstname AS firstname_19, t16.lastname AS lastname_20, t16.screen_name AS screen_name_21, t16.function_title AS function_title_22, t16.mobile_number AS mobile_number_23, t16.birthday AS birthday_24, t16.age AS age_25, t16.zipcode AS zipcode_26, t16.city AS city_27, t16.id AS id_28, t16.s_uuid AS s_uuid_29, t16.user_id AS user_id_30 FROM user t0 LEFT JOIN user_extra t16 ON t16.user_id = t0.id WHERE t0.email = ? LIMIT 1",
"params": [
"some@email.com"
],
"types": [
"string"
],
"executionMS": 0.0007460117340087891
},
"3": {
"sql": "\"START TRANSACTION\"",
"params": null,
"types": null,
"executionMS": 0.00010800361633300781
},
"4": {
"sql": "INSERT INTO user_payments (status, payment_provider_id, payment_method, quantity, price_total, created_on, updated_on, paid_on, id, s_uuid, user_id, product_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
"params": {
"1": "open",
"2": "tr_S432rV6fhM",
"3": "paypal",
"4": 1,
"5": "17.95",
"6": "2019-10-24T07:27:22+00:00",
"7": null,
"8": null,
"9": "9dd4af76-f630-11e9-90f2-024216133c1a",
"10": "9dd4af76-f630-11e9-90f2-024216133c1a",
"11": "c2abb28c-f62f-11e9-b71f-024216133c1a",
"12": "bce559e8-f5bc-11e9-85f8-024216133c1a"
},
"types": {
"1": "string",
"2": "string",
"3": "string",
"4": "integer",
"5": "decimal",
"6": "datetime",
"7": "datetime",
"8": "datetime",
"9": "uuid_binary_ordered_time",
"10": "uuid",
"11": "uuid_binary_ordered_time",
"12": "uuid_binary_ordered_time"
},
"executionMS": 0.0003437995910644531
},
"5": {
"sql": "DELETE FROM payment_pending WHERE id = ?",
"params": [
"b836c012-f62f-11e9-80b0-024216133c1a"
],
"types": [
"uuid_binary_ordered_time"
],
"executionMS": 0.0003409385681152344
},
"6": {
"sql": "\"COMMIT\"",
"params": null,
"types": null,
"executionMS": 0.09281802177429199
}
},
"enabled": true,
"start": 1571902427.83485,
"currentQuery": 6
解决方案
为什么不像这样在坚持后立即使用冲洗:
$values = [
"val1", "val2", "val3"
];
foreach ($values as $value) {
$i = new MyEntityClass();
$i->setVerified(false);
$i->setName($value);
$this->em->persist($i);
$this->em->flush();
}
或更好:
public function save(MyEntityClass $myEntityClass): void
{
$this->em->persist($myEntityClass);
$this->em->flush();
}
在实体的存储库中,并在您要单独保存相关实体时使用它
编辑 - 另一种尝试方式
$values = [
"val1", "val2", "val3"
];
$this->em->getConnection()->beginTransaction();
foreach ($values as $value) {
$i = new MyEntityClass();
$i->setVerified(false);
$i->setName($value);
$this->em->persist($i);
}
try {
$this->em->flush();
$this->em->getConnection()->commit();
} catch (Exception $e) {
$this->em->getConnection()->rollback();
$this->em->close();
throw $e;
}
还在 $id 的定义中添加行:
/**
* @var \Ramsey\Uuid\UuidInterface
*
* @ORM\Id
* @ORM\Column(type="uuid_binary_ordered_time", unique=true)
* @GeneratedValue(strategy="CUSTOM")
* @CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidOrderedTimeGenerator")
* @Groups({"uuid"})
*/
protected $id;
编辑 - 使用 type="uuid"
更新 $id 的声明
/**
* Entity constructor.
*
* @param UuidInterface|null $uuid
*
* @throws \Exception
*/
public function __construct(UuidInterface $uuid = null)
{
if (null === $uuid) {
$uuid = Uuid::uuid4();
}
$this->id = $uuid;
[Other fields ...]
}
/**
* @var \Ramsey\Uuid\UuidInterface
*
* @ORM\Id
* @ORM\Column(type="uuid")
*/
protected $id;
/**
* @return UuidInterface|null
*/
public function getId(): ?UuidInterface
{
return $this->id;
}
这样做你应该排除它是 $id 与 Doctrine 混淆......我们可以在其他地方进行调查
推荐阅读
- openapi - 如何使用 openapi3 yml 检查电子邮件或密码是否存在
- excel - 不存在的 Vlookup 引用工作表
- sql - 特殊字符(夏威夷 'Okina)的 SQL Server 2017 排序规则导致“?” 字符串行为
- javascript - 如何更改 v-data-table 中的单行(不是样式,内容)
- javascript - 使用正则表达式在 html 页面上获取 sitekey
- javascript - JQuery 无法分配给“#back”上的属性“guid”:不是对象
- python - 使用多处理从另一个类调用类方法的实例
- macos - 如何让 sshuttle 在其他 VPN 服务的专用 IP 上工作?
- java - Java 在 URLConnection.java 类中找不到导入
- node.js - nodemon:在heroku上找不到