-->

Symfony 4. InheritanceType(“JOINED”) and ParamConv

2020-05-09 01:39发布

问题:

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.



标签: symfony4