Symfony API platform - nelze přidat relační záznam - An error occurred - Invalid IRI

Autor Cryogenic
Publikováno 14.04.2021
Kategorie programovani
Stav Vyřešeno
Dobrý den,

použivám Symfony API platform a když chci přidat relační záznam, tak dostávám chybu:

exception":"[object] (Symfony\\Component\\Serializer\\Exception\\UnexpectedValueException(code: 0): Invalid IRI \"879\". at

Nedaří se zkrátka odeslat vazební (relační) subdata na existujici data pres vazebni 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":


API očekává dle 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;




Zdrojová data sestavuji takto:
------------------------------------------------------------------

$data = [
"data" => $slug,
"identifier" => 'Km',
"value" => $r->km,
];



V logu /var/log/dev.log je:
------------------------------------------------------------------
[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)"} []



DODATEK: dalsi chyba
------------------------------------------------------------------
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":










Odpovědi na dotaz

The English language is translated by machine - the translator can modify eg cited codes = it is better to use codes from the Czech original.

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":









RE: Symfony API platform - nelze přidat relační…

Autor Cryogenic
Publikováno 13.05.2021
Kategorie programovani
Stav Zkontrolováno
Ahoj,

nikde jsem se to nedočetl, až na jednom blogu:

Nestačí pouze uvést vazební (relační) id, ale je třeba se v API platform odkát na celou API cestu k relačnímu záznamu ve tvaru '/api/streams/555, takze data musí vypadat takto:

$data = [
"stream" => '/api/streams/'. $slug,
"identifier" => 'Km',
"value" => (string) $r->km,
"suser" => 2,

];



A dotatkovou chybu The type of the value attribute must be string, integer given jsem vyřešil takto:
"value" => (string) $r->km,


The English language is translated by machine - the translator can modify eg cited codes = it is better to use codes from the Czech original.

Symfony API platform - cant add a related record - An error occurred - Invalid IRI

510 / 5000
Výsledky překladu
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,
Tento web používá k poskytování služeb, personalizaci reklam a analýze návštěvnosti soubory cookie. Používáním tohoto webu s tím souhlasíte. Další informace