Symfony returns 404 'object not found by EntityValueResolver'
"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.
Fix it fast
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;2Match what you see, then jump to its fix
- 1
Message ends after EntityValueResolver with a full stop, route uses {id}No row matches the value in the URLFix - 2
The 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
Cannot 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
The message ends with: The expression "..." returned null.A MapEntity expression returned nullFix
- 1
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.local2Fix
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/42Confirmed by symfony.com
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.php2Fix
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-shirtConfirmed by symfony.com
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_show2Fix
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/42Confirmed by raw.githubusercontent.com
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/42Confirmed by raw.githubusercontent.com
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 fixBack end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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 fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fix