Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Symfony returns 404 'object not found by EntityValueResolver'

Error"App\Entity\Products" object not found by "Symfony\Bridge\Doctrine\ArgumentResolver\EntityValueResolver".

Short answer

The EntityValueResolver looked for the entity from your route and got null, and the controller argument is not nullable, so Symfony throws a 404. Either the row does not exist, the route value is not what the lookup uses (default is the id), or an expression returned null. Map the route value explicitly.

  • Applies to Symfony 7.3, Doctrine ORM 3, PHP 8.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the route and the row. The route shows which wildcard names exist, and a count query shows whether the row is in the database the app really uses.

    php bin/console debug:router app_product_show
    # in your SQL client: SELECT id FROM products WHERE id = 42;
  2. 2Match what you see, then jump to its fix

    1. 1Message ends after EntityValueResolver with a full stop, route uses {id}No row matches the value in the URLFix
    2. 2The URL has a slug such as /product/blue-shirt and the route is {id} or {slug}The route value is a slug or code, not the primary keyFix
    3. 3Cannot find mapping for "App\Entity\Products": declare one using either the #[MapEntity] attribute or mapped route parametersThe wildcard has a different name, so the resolver has nothing to useFix
    4. 4The message ends with: The expression "..." returned null.A MapEntity expression returned nullFix

What does the error look like?

  • "App\Entity\Products" object not found by "Symfony\Bridge\Doctrine\ArgumentResolver\EntityValueResolver".
  • Cannot find mapping for "App\Entity\Products": declare one using either the #[MapEntity] attribute or mapped route parameters.
  • The expression "repository.find(product_id)" returned null.

Cause 1 of 4

No row matches the value in the URL

1Check

With a {id} wildcard the resolver calls find() on the primary key, and a miss becomes a 404. Check the value in the URL against the table, and check that .env points at the database you expect.

# in your SQL client: SELECT id FROM products ORDER BY id DESC LIMIT 5;
grep DATABASE_URL .env .env.local

2Fix

If missing rows are a normal case, make the argument nullable and answer in your own code. Otherwise fix the link or the data.

#[Route('/product/{id}', name: 'app_product_show')]
public function show(?Product $product): Response
{
    if (null === $product) {
        throw $this->createNotFoundException('No such product.');
    }
    // ...
}

3Verify

An existing id returns 200, a missing one your own 404 text.

curl -I http://localhost:8000/product/42

Confirmed by symfony.com

Did this fix it?

Cause 2 of 4

The route value is a slug or code, not the primary key

1Check

By default the wildcard is used as the id. For another field, name the property in the route as {param:argument} so the resolver runs findOneBy() on it.

grep -n "#\[Route" src/Controller/ProductController.php

2Fix

Map the wildcard to the entity property.

// performs findOneBy(['slug' => $slug])
#[Route('/product/{slug:product}', name: 'app_product_show')]
public function show(Product $product): Response
{
    // ...
}

3Verify

Open the URL with a known slug.

curl -I http://localhost:8000/product/blue-shirt

Confirmed by symfony.com

Did this fix it?

Cause 3 of 4

The wildcard has a different name, so the resolver has nothing to use

1Check

A wildcard such as {product_id} is not {id}, and it does not match an entity property. The resolver then has no criteria. Compare the wildcard names in debug:router with the entity's properties.

php bin/console debug:router app_product_show

2Fix

Tell the resolver which wildcard is the id.

#[Route('/product/{product_id}', name: 'app_product_show')]
public function show(
    #[MapEntity(id: 'product_id')]
    Product $product
): Response {
    // ...
}

3Verify

The page loads for an existing id.

curl -I http://localhost:8000/product/42

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 4 of 4

A MapEntity expression returned null

1Check

With expr, the resolver evaluates your expression, where route wildcards are variables, and 404s when it returns null. The repository method may not exist for that value, or the variable may hold something else than you expect.

grep -rn "MapEntity(expr" src/

2Fix

Correct the expression, or use the id or mapping option when the lookup is a plain field.

#[Route('/product/{product_id}')]
public function show(
    #[MapEntity(expr: 'repository.find(product_id)')]
    Product $product
): Response {
    // ...
}

3Verify

Request the URL with a value you know exists.

curl -I http://localhost:8000/product/42

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Name the wildcard {id}, or use {slug:product} for another field, so no extra attribute is needed.
  • Use the message option of MapEntity for a readable 404 text in the dev environment.
  • Add a functional test that requests one existing and one missing id.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Symfony: 'controller cannot be fetched from the container because it is private'

Controller "App\Controller\HelloController" cannot be fetched from the container because it is private. Did you forget to tag the service with "controller.service_arguments"?

Symfony fetches controllers from the container by id, and private services cannot be fetched, so the controller must be marked as a controller.

3 causes, each with a fix

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.