Reputation: 1156
I'm starting with API Platform and I'm using the example entity "Foo":
namespace AppBundle\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;
/**
* Foo
*
* @ORM\Table(name="foo")
* @ApiResource
* @ORM\Entity
*/
class Foo
{
/**
* @var string
*
* @ORM\Column(name="bar", type="string", length=255, nullable=false)
*/
private $bar;
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
}
I succesfully see the new Entity in dashboard (See image):
And I had the following error when I try to get all Foo collection:
curl -X GET "http://127.0.0.1:8000/foos" -H "accept: application/json"
{
"type": "https://tools.ietf.org/html/rfc2616#section-10",
"title": "An error occurred",
"detail": "Unable to generate an IRI for the item of type \"AppBundle\\Entity\\Foo\"",
"trace": [
{
"namespace": "",
"short_class": "",
"class": "",
"type": "",
"function": "",
"file": "/home/xxxx/api/vendor/api-platform/core/src/Bridge/Symfony/Routing/IriConverter.php",
"line": 107,
"args": []
},
I tried different formats and check the the routes too:
julian@js:~/xxxxx/api$ ./bin/console debug:router
------------------------------- -------- -------- ------ -----------------------------------
Name Method Scheme Host Path
------------------------------- -------- -------- ------ -----------------------------------
_wdt ANY ANY ANY /_wdt/{token}
_profiler_home ANY ANY ANY /_profiler/
_profiler_search ANY ANY ANY /_profiler/search
_profiler_search_bar ANY ANY ANY /_profiler/search_bar
_profiler_phpinfo ANY ANY ANY /_profiler/phpinfo
_profiler_search_results ANY ANY ANY /_profiler/{token}/search/results
_profiler_open_file ANY ANY ANY /_profiler/open
_profiler ANY ANY ANY /_profiler/{token}
_profiler_router ANY ANY ANY /_profiler/{token}/router
_profiler_exception ANY ANY ANY /_profiler/{token}/exception
_profiler_exception_css ANY ANY ANY /_profiler/{token}/exception.css
_twig_error_test ANY ANY ANY /_error/{code}.{_format}
api_entrypoint ANY ANY ANY /{index}.{_format}
api_doc ANY ANY ANY /docs.{_format}
api_jsonld_context ANY ANY ANY /contexts/{shortName}.{_format}
api_foos_get_collection GET ANY ANY /foos.{_format}
api_foos_post_collection POST ANY ANY /foos.{_format}
api_foos_get_item GET ANY ANY /foos/{id}.{_format}
api_foos_put_item PUT ANY ANY /foos/{id}.{_format}
api_foos_delete_item DELETE ANY ANY /foos/{id}.{_format}
------------------------------- -------- -------- ------ -----------------------------------
It's possible what I need some special library in my server? What I'm doing wrong?
Thanks in advance!
Upvotes: 12
Views: 27523
Reputation: 3085
For future visitors; the PHP 8.4 property hooks also seem to result in this error on the creation of a resource.
#[ORM\Id]
#[ORM\Column(type: 'integer')]
#[ORM\GeneratedValue(strategy: 'SEQUENCE')]
private ?int $id = null {
get {
return $this->id;
}
}
The probable cause is that the ORM can not hydrate the freshly created object with the persistence layer's newly generated identifier after flush, but will not make the request throw an exception either.
Upvotes: 0
Reputation: 881
interesting that it seems like it's obligatory to add get word in getter, so you can't have id() but getId()
Upvotes: -1
Reputation: 7606
Adding an answer as it may help other people, I had a typo in my uriTemplate
parameter, and used {format}
instead of {_format}
which caused the $this->router->generate
to fail in vendor/api-platform/core/src/Symfony/Routing/IriConverter.php
.
uriTemplate: '/users/{user_id}/subresource.{_format}',
To debug this, comment the try/catch in the IriConverter.php
class to see the exact error.
Upvotes: 0
Reputation: 11
I met this issue during the implementation of "API Platform + ElasticSearch".
Debugged the exception I found more clearer explanation: "Not able to retrieve identifiers. Unable to generate an IRI for the item of type "App\Entity\..."
In my case entity didn't have a setId() method.
public function setId(int $id): void
{
$this->id = $id;
}
It has helped me.
Upvotes: 1
Reputation: 23
I was passing for a similar problem, and I was having a big difficulty to find a solution.
Just to help someone that couldn't find some answer, I suggest going deeper and put some debug/dump inside the IriConverter class to see what is really happening.
I discovered that my main class was ok, but another a relation/linked class had a problem. So sometimes could be in another place but with the same error.
Upvotes: 1
Reputation: 899
For future visitors:
You'll get this error allways if you disable json-ld format. Check your configuration file ./config/packages/api_platform.yaml:
formats:
json: ['application/json']
jsonld: ['application/ld+json']
jsonapi: ['application/vnd.api+json']
html: ['text/html']
csv: ['text/csv']
This error can be launched for several reasons. Other one happens when you declare an entity without ORM and you forgot declare its ItemDataProvider
Upvotes: 0
Reputation: 590
Remove your existing entity and create new model with bin/console make:entity has worked for me !
Upvotes: -3
Reputation: 2157
You're missing a getter for your id
public function getId()
{
return $this->id;
}
Upvotes: 18