Symfony 4. InheritanceType(“JOINED”) and ParamConverter. Strange phenomenon

我与影子孤独终老i 提交于 2019-12-04 05:36:29

问题


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 
{
.
.
.
}

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)
{
.
.
.
}

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

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

"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",

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?


回答1:


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.



来源:https://stackoverflow.com/questions/55172359/symfony-4-inheritancetypejoined-and-paramconverter-strange-phenomenon

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!