Symfony 4. InheritanceType(“JOINED") 和 ParamConverter.奇怪的现象 [英] Symfony 4. InheritanceType("JOINED") and ParamConverter. Strange phenomenon

查看:24
本文介绍了Symfony 4. InheritanceType(“JOINED") 和 ParamConverter.奇怪的现象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经定义了类 CoreCase

I have defined the class CoreCase

/**
 * @ORM\Entity(repositoryClass="App\Repository\CoreCaseRepository")
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="type", type="string")
 * @ORM\DiscriminatorMap({"Diesel" = "DieselCase", "Carloan" = "CarloanCase"})
 * @ORM\HasLifecycleCallbacks()
 * 
 */
abstract class CoreCase 
{
.
.
.
}

还有两个类 DieselCase 和 Carloan:

and two classes DieselCase and Carloan:

/**
 * @ORM\Entity
 * @ORM\HasLifecycleCallbacks()
 */
class DieselCase extends CoreCase
{
.
.
.
}

/**
 * @ORM\Entity(repositoryClass="App\Repository\CarloanCaseRepository")
 * @ORM\HasLifecycleCallbacks()
 */
class CarloanCase extends CoreCase
{
.
.
.
}

在动作中,我使用了参数转换器

In the action, I'm using the param converter

/**
 * @Rest\Get("/case/carloan/{case}", requirements={"case" = "\d+"})
 *
 */
public function getCarloanCase(CarloanCase $case)
{
.
.
.
}

它对我有用,如果我使用现有的 CarloanCase 调用 URL,例如/case/carloan/201

It works for me, if I call the URL, with an existing CarloanCase, for example /case/carloan/201

如果我用 DieselCase 的 id 调用 URL,我预计会出现 404 错误,但我得到 500 错误

If I call the URL with an id of a DieselCase, I expect a 404-error, but I get the 500-error

"message": "传递给 App\Controller\Api\Cases\CarloanController::getCarloanCase() 的参数 1 必须是 App\Entity\Cases\CarloanCase 的实例,给出的 App\Entity\Cases\DieselCase 实例,在/home/alexander/projects/lawbutler/vendor/symfony/http-kernel/HttpKernel.php 第 150 行调用",

"message": "Argument 1 passed to App\Controller\Api\Cases\CarloanController::getCarloanCase() must be an instance of App\Entity\Cases\CarloanCase, instance of App\Entity\Cases\DieselCase given, called in /home/alexander/projects/lawbutler/vendor/symfony/http-kernel/HttpKernel.php on line 150",

但是!如果我从 Carloan 注释中删除 (repositoryClass="App\Repository\CarloanCaseRepository"),它可以正常工作,并且会得到预期的 404 错误.为什么行为如此奇怪?我能做什么?

BUT! If I remove (repositoryClass="App\Repository\CarloanCaseRepository")from the Carloan annotation, it works correctly, and I get the expected 404-error. Why is the behavior so strange? What can I do?

推荐答案

我会在这里猜测一下,虽然我同意这很奇怪.我注意到您没有 DieselCase 的自定义存储库来与 CarloanCase 并行.会不会,出于某种奇怪的原因,CarloanCaseRepository 被错误地用于 DieselCase 查询?我认为这可以解释 500 错误的原因,因为您交付的实体类型错误.可以肯定的是,我会在该 repo 中放入一些 xdebug 断点,以查看它在这种情况下是如何使用的.另外,我会尝试添加自定义/特定 DieselCaseRepository 以查看是否可以修复错误.(从长远来看,我希望你会想要那个.

I'll take a guess here, although I agree it's weird. I notice you don't have a custom repository for the DieselCase to parallel that for the CarloanCase. Could it be, that for some odd reason, the CarloanCaseRepository is being picked up, mistakenly for the DieselCase query? I think it would explain the cause of the 500 error, in that you're getting the wrong kind of entity delivered. To be sure, I'd put in some xdebug breakpoints in that repo to see how it's getting used in that circumstance. Also, I'd try adding a custom/specific DieselCaseRepository to see if that fixes the error. (I'd expect you'd want that in the long run anyway.

这篇关于Symfony 4. InheritanceType(“JOINED") 和 ParamConverter.奇怪的现象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆