我有一个简单的数组,其中包含需要插入到数据库中的某些值。在所有的值中,只有最后一个被插入。
当我把 flush();
放在循环内时,记录确实被插入了。
当我在 flush 之前(循环外部)dump 实体管理器时,我确实看到了对所有值(实体)的引用。
尽管如此,只有最后一条记录被插入。它得到了 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();
更新:
我为"pre"和"post"刷新事件添加了监听器。
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 dump 为空。
更新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