Search code examples
phpsymfonysymfony4

Route function argument with default = null always being injected even when not passed in the URL


In an old Symfony 4.4 application I inheritted, I have two entities, Influencer and InfluencerManagement.

The Influencer has an influencerManagement (represented by influencer_management_id in its DB table).

Then I have this route in a controller:

/**
 * @Route("/influencer_management/{influencerManagement}/managers/{influencer}", name="influencer_management_managers", requirements={"influencerManagement"="\d+"})
 */
public function managers(InfluencerManagement $influencerManagement, ?Influencer $influencer = null)
{
    dd($influencer);
    // ...

When I go to, for example, /influencer_management/49/managers/ (no influencer passed on the URL's last section), the dd call still shows me an influencer - the first one on the table that has influencer_management_id set to 49.

I have no idea why it is instantiating the variable when I'm explicitly telling it to default to null if no parameter is passed there. I'm especially confused as to how it "knows" to fetch an Influencer that is related to the InfluencerManagement. That led me to think it could have something to do with a Repository or an Entity Manager somewhere, but as far as I can tell, none of those things are involved.

Any ideas as to where I should look would be greatly appreciated.


Solution

  • this behavior Entity Value Resolver

    Maybe you need to use MapEntity attribute in these place ?