Symfony API platform - cant add a related record - An error occurred - Invalid IRI
Hi,
I use the Symfony API platform and when I want to add a relational record, I get an error:
exception":"[object] (Symfony\\Component\\Serializer\\Exception\\UnexpectedValueException(code: 0): Invalid IRI \"879\". at
In short, it is not possible to send binding (relational) subdata to existing data via binding ID 879
JSON request:
------------------------------------------------------------------
array
data => "879" (3)
identifier => "Km" (2)
value => 55.0
Response ERROR
------------------------------------------------------------------
{"@context":"\/api\/contexts\/Error","@type":"hydra:Error","hydra:title":"An error occurred","hydra:description":"Invalid IRI \u0022879\u0022.","trace":
The API expects according to http
------------------------------------------------------------------
POST
/api/subdatas
Creates a subdata resource.
Parameters
Try it out
No parameters
Request body
application/ld+json
The new subdata resource
Example Value
Schema
{
"data": "string",
"identifier": "string",
"value": "string"
}
Entita subdata
------------------------------------------------------------------
* @ApiResource(
* collectionOperations={"get"={"normalization_context"={"groups"="data:list"}},"post"},
* itemOperations={"get"={"normalization_context"={"groups"="data:item"}},"delete","put"},
* order={"date"="DESC", "type"="ASC"},
* paginationEnabled=true,
* attributes={
* "pagination_items_per_page"=10
* }
* )
.............
/**
* @ORM\ManyToOne(targetEntity=data::class, inversedBy="subdata")
* @ORM\JoinColumn(nullable=false)
*
* @Groups({"subdata:list", "subdata:item"})
*/
private $data;
I compile the source data as follows:
------------------------------------------------------------------
$data = [
"data" => $slug,
"identifier" => 'Km',
"value" => $r->km,
];
In log /var/log/dev.log is:
------------------------------------------------------------------
[2021-04-13T10:39:35.332291+02:00] request.CRITICAL: Uncaught PHP Exception Symfony\Component\Serializer\Exception\UnexpectedValueException: "Invalid IRI "879"." at /var/www/symfony/webapp5/vendor/api-platform/core/src/Serializer/AbstractItemNormalizer.php line 431 {"exception":"[object] (Symfony\\Component\\Serializer\\Exception\\UnexpectedValueException(code: 0): Invalid IRI \"879\". at /var/www/symfony/webapp5/vendor/api-platform/core/src/Serializer/AbstractItemNormalizer.php:431)\n[previous exception] [object] (ApiPlatform\\Core\\Exception\\InvalidArgumentException(code: 0): No route matches \"879\". at /var/www/symfony/webapp5/vendor/api-platform/core/src/Bridge/Symfony/Routing/IriConverter.php:77)\n[previous exception] [object] (Symfony\\Component\\Routing\\Exception\\ResourceNotFoundException(code: 0): No routes found for \"/879/\". at /var/www/symfony/webapp5/vendor/symfony/routing/Matcher/Dumper/CompiledUrlMatcherTrait.php:70)"} []
APPENDIX: another error
------------------------------------------------------------------
The type of the value attribute must be string, integer given
JSON: {"@context":"\/api\/contexts\/Error","@type":"hydra:Error","hydra:title":"An error occurred","hydra:description":"The type of the \u0022value\u0022 attribute must be \u0022string\u0022, \u0022integer\u0022 given.","trace":
Hello,
I haven't read it anywhere except on one blog:
It is not enough to just specify the binding (relational) id, but in the API platform it is necessary to refer to the entire API path to the relational record in the form '/api/streams/555, so the data must look like this:
$data = [
"stream" => '/api/streams/'. $slug,
"identifier" => 'Km',
"value" => (string) $r->km,
"suser" => 2,
];
And I solved the additional error The type of the value attribute must be string, integer given as follows:
I use the Symfony API platform and when I want to add a relational record, I get an error:
exception":"[object] (Symfony\\Component\\Serializer\\Exception\\UnexpectedValueException(code: 0): Invalid IRI \"879\". at
In short, it is not possible to send binding (relational) subdata to existing data via binding ID 879
JSON request:
------------------------------------------------------------------
array
data => "879" (3)
identifier => "Km" (2)
value => 55.0
Response ERROR
------------------------------------------------------------------
{"@context":"\/api\/contexts\/Error","@type":"hydra:Error","hydra:title":"An error occurred","hydra:description":"Invalid IRI \u0022879\u0022.","trace":
The API expects according to http
------------------------------------------------------------------
POST
/api/subdatas
Creates a subdata resource.
Parameters
Try it out
No parameters
Request body
application/ld+json
The new subdata resource
Example Value
Schema
{
"data": "string",
"identifier": "string",
"value": "string"
}
Entita subdata
------------------------------------------------------------------
* @ApiResource(
* collectionOperations={"get"={"normalization_context"={"groups"="data:list"}},"post"},
* itemOperations={"get"={"normalization_context"={"groups"="data:item"}},"delete","put"},
* order={"date"="DESC", "type"="ASC"},
* paginationEnabled=true,
* attributes={
* "pagination_items_per_page"=10
* }
* )
.............
/**
* @ORM\ManyToOne(targetEntity=data::class, inversedBy="subdata")
* @ORM\JoinColumn(nullable=false)
*
* @Groups({"subdata:list", "subdata:item"})
*/
private $data;
I compile the source data as follows:
------------------------------------------------------------------
$data = [
"data" => $slug,
"identifier" => 'Km',
"value" => $r->km,
];
In log /var/log/dev.log is:
------------------------------------------------------------------
[2021-04-13T10:39:35.332291+02:00] request.CRITICAL: Uncaught PHP Exception Symfony\Component\Serializer\Exception\UnexpectedValueException: "Invalid IRI "879"." at /var/www/symfony/webapp5/vendor/api-platform/core/src/Serializer/AbstractItemNormalizer.php line 431 {"exception":"[object] (Symfony\\Component\\Serializer\\Exception\\UnexpectedValueException(code: 0): Invalid IRI \"879\". at /var/www/symfony/webapp5/vendor/api-platform/core/src/Serializer/AbstractItemNormalizer.php:431)\n[previous exception] [object] (ApiPlatform\\Core\\Exception\\InvalidArgumentException(code: 0): No route matches \"879\". at /var/www/symfony/webapp5/vendor/api-platform/core/src/Bridge/Symfony/Routing/IriConverter.php:77)\n[previous exception] [object] (Symfony\\Component\\Routing\\Exception\\ResourceNotFoundException(code: 0): No routes found for \"/879/\". at /var/www/symfony/webapp5/vendor/symfony/routing/Matcher/Dumper/CompiledUrlMatcherTrait.php:70)"} []
APPENDIX: another error
------------------------------------------------------------------
The type of the value attribute must be string, integer given
JSON: {"@context":"\/api\/contexts\/Error","@type":"hydra:Error","hydra:title":"An error occurred","hydra:description":"The type of the \u0022value\u0022 attribute must be \u0022string\u0022, \u0022integer\u0022 given.","trace":
REPLY
Hello,
I haven't read it anywhere except on one blog:
It is not enough to just specify the binding (relational) id, but in the API platform it is necessary to refer to the entire API path to the relational record in the form '/api/streams/555, so the data must look like this:
$data = [
"stream" => '/api/streams/'. $slug,
"identifier" => 'Km',
"value" => (string) $r->km,
"suser" => 2,
];
And I solved the additional error The type of the value attribute must be string, integer given as follows:
"value" => (string) $r->km,