【问题标题】:Doctrine FindOneBy with exact DateTime return null具有确切日期时间的学说 FindOneBy 返回 null
【发布时间】:2020-09-28 13:11:10
【问题描述】:

我有两张桌子,ordersappointment

CREATE TABLE `orders` (
  `id` int(10) UNSIGNED NOT NULL,
  `id_buyer` int(10) UNSIGNED DEFAULT NULL,
  `id_seller` int(10) UNSIGNED DEFAULT NULL,
  `reference` varchar(16) COLLATE utf8mb4_unicode_ci NOT NULL,
  `email` varchar(128) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `first_appointment` datetime DEFAULT NULL,
  `status` smallint(5) UNSIGNED NOT NULL,
  `creation_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `paid_date` datetime DEFAULT NULL,
  `fulfilled_date` datetime DEFAULT NULL,
  `returned_date` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `appointment` (
  `id` int(10) UNSIGNED NOT NULL,
  `id_owner` int(10) UNSIGNED DEFAULT NULL,
  `id_customer` int(10) UNSIGNED DEFAULT NULL,
  `start` datetime NOT NULL,
  `end` datetime NOT NULL,
  `online` tinyint(1) NOT NULL,
  `participant_number` smallint(5) UNSIGNED NOT NULL,
  `status` smallint(6) NOT NULL,
  `creation_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

这两个表都是由 Symfony 和教义使用实体自动创建的。

当我尝试创建自定义 sql 查询时,一切正常:

SELECT `id`, `id_owner`, `id_customer`, `start`  FROM `appointment` WHERE `id_owner` = 20 AND `id_customer` = 1 AND `start` = '2020-06-06 10:00:00'
+-----+----------+-------------+---------------------+
| id  | id_owner | id_customer | start               |
+-----+----------+-------------+---------------------+
| 763 |       20 |           1 | 2020-06-06 10:00:00 |
+-----+----------+-------------+---------------------+
1 row in set (0.00 sec)

现在,这是我的问题,当我执行与上述查询相同的操作时,但使用学说中的 findOneBy 时,它返回 null。

$a = $repo->findOneBy(['start'=> $order->getFirstAppointment(), 'owner' => $order->getSeller(), 'customer' => $order->getBuyer()]);
var_dump($a);
var_dump($o->getFirstAppointment());
var_dump($order->getSeller()->getId());
var_dump($order->getBuyer()->getId());

这里是 var_dump 输出

ExampleController.php:390:null

ExampleController.php:391:
object(DateTime)[2692]
  public 'date' => string '2020-06-16 10:00:00.000000' (length=26)
  public 'timezone_type' => int 3
  public 'timezone' => string 'UTC' (length=3)

ExampleController.php:392:int 20

ExampleController.php:393:int 1

当我不使用日期时,一切正常,但是我需要使用日期。我的猜测是,这是因为日期 '2020-06-16 10:00:00.000000' 末尾有一些零,但是当我查看数据库时,“.000000”没有显示在任何地方。

mysql> SELECT `first_appointment` FROM orders WHERE id = 13;
+---------------------+
| first_appointment   |
+---------------------+
| 2020-06-16 10:00:00 |
+---------------------+
1 row in set (0.00 sec)

有谁知道如何在不使用教义的情况下解决问题?另外,我很想知道为什么它不起作用,这让我发疯。

为了完整起见,我正在使用:

  • PHP 7.4.3
  • 教义/通用 3.0.2
  • Symfony v5.0.11

我也研究过类似的问题,例如 this,但他们使用的是 BETWEEN 日期而不是完全匹配的日期。

【问题讨论】:

  • 这可能是您的问题而不是您的代码中的拼写错误,但您的示例数据库行显示 2020-06-06,并且您的 DateTime 对象显示 2020-06- 16.如果您尝试使用 only 日期参数的findOneBy,您会得到任何结果吗?
  • 不,是因为打错字我快疯了

标签: php mysql symfony doctrine-orm


【解决方案1】:

如果使用标量值会发生什么。所以字符串格式的直接id和日期时间

$a = $repo->findOneBy([
    'start'=> $order->getFirstAppointment()->format('Y-m-d H:i:s'), 
    'owner' => $order->getSeller()->getId(), 
    'customer' => $order->getBuyer()->getId()
]);






或使用 DQL 进行更多控制

$query = $em->createQuery( 'SELECT u FROM Namespace\\EntityName u WHERE u.start = :start AND u.owner = :owner AND u.customer = :customer' );
                    
$query->setParameters([
     'start' => $order->getFirstAppointment()->format('Y-m-d H:i:s') ,
     'owner' => $order->getSeller()->getId(),
     'customer' => $order->getBuyer()->getId()

 ]);
$result = $query->getResult();

作为一个快速提示。将 ID 键用于关系映射可能是一个更好的主意。在 Order 中,您将使用从 Order 到约会 ID 而不是日期的一对一关系。

【讨论】:

  • 无法将“字符串”类型的 PHP 值“2020-06-16 10:00:00”转换为“日期时间”类型。预期为以下类型之一:null、DateTime
  • 您准备好使用 findOneBy 了吗?你可以使用 dql 。我会用一个例子来更新我的答案
  • 使用 DQL 版本给我一个空数组作为 $result。 array (size=0) empty。我已将查询更改为'SELECT a FROM App\\Entity\\Appointment a WHERE a.start = :start AND a.owner = :owner AND a.customer = :customer'。哦,我不能使用 1-1 关系,因为...它很长很复杂。
  • 您是否验证了数据库中的数据。日期相同吗? (查看 iainn 对您的帖子的评论)您的约会->开始是 2020-06-06 10:00:00 并且您的订单->first_appointment 是 2020-06-16 10:00:00
猜你喜欢
  • 2016-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-24
  • 1970-01-01
  • 2021-06-19
  • 2017-06-23
  • 2016-01-17
相关资源
最近更新 更多