gpt4 book ai didi

postgresql - 无效的文本表示 : 7 ERROR: invalid input syntax for uuid: "test"

转载 作者:行者123 更新时间:2023-11-29 14:09:44 25 4
gpt4 key购买 nike

我将 Symfony 3.2 与 doctrine 和 postgresql 一起使用。

我创建了一个以 uuid 作为主键的实体。

我的实体定义:

/**
* Booking
*
* @ORM\Table(name="booking")
* @ORM\Entity(repositoryClass="AppBundle\Repository\BookingRepository")
* @ORM\EntityListeners({"AppBundle\EventListener\BookingListener"})
*/
class Booking {
/**
* @var string
*
* @ORM\Column(type="guid")
* @ORM\Id
* @ORM\GeneratedValue(strategy="UUID")
*/
private $id;
}

在我的 Controller 中,我有一个这样的表演 Action :

/**
* @Route("booking/{id}", name="booking_show")
* @Method({"GET"})
*/
public function showAction(Request $request, Booking $booking) {
...
}

一切似乎都工作正常,但是当我尝试加载一个将错误值作为 ID(即 /booking/hello123)的路由时,我收到:

SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for uuid: "hello123"

相反,我会期待 404。

有没有办法捕获这个异常并重定向到 404 页面?

最佳答案

您可以使用 Route Requirements - 您可以指定您的参数需要匹配哪些条件才能“符合”特定路线。这个需求是一个正则表达式,所以你需要做的就是为一个UUID写一个正则表达式

/**
* @Route("booking/{id}", name="booking_show", requirements={"id": "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"})
* @Method({"GET"})
*/
public function showAction(Request $request, Booking $booking) {
...
}

注意:上面使用的正则表达式只是我在 Google 中为 UUID 正则表达式找到的第一个结果,我没有验证它是否有效

最后,如果你的 id 不匹配正则表达式,它不匹配路由,你应该得到 404。

关于postgresql - 无效的文本表示 : 7 ERROR: invalid input syntax for uuid: "test",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43777878/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com