Core: array of IRIs

Created on 31 May 2016  路  9Comments  路  Source: api-platform/core

Hello everyone.
I have a problem with my API and you do not get solution

this is one of the entities
documentacion

in my schema.yml is written as follows

properties:
      languages: { range: "language", cardinality: (0..*)}
      writingExpertise: { range: "Rating", cardinality: (0..1)}
      expertiseSpokenLanguage: { range: "Rating", cardinality: (0..1)}
      levelOfAdaptation: { range: "Rating", cardinality: (0..1)}
      employee: { range: "Person", cardinality: (1..1)}

I am using for testing Postman, I'm trying to persist in the languages attribute data
error

It does not say that there is error, but no values are stored. I have tried several ways but does not work and do not understand why

Someone help me?

Thanks in advance

Most helpful comment

@Fercho191 Do you understand the error message ?

It means that you are trying to pass an array and you have an object in the set/get.

Did you generate the get/set by hand or the schema-generator did it ?

All 9 comments

Hello @Fercho191,

Could you please add some precision about the version of ApiPlatform you are using ?

Could you please show us the entity generated by the schema ?

Can you copy the code of generated entities?

This is the code of the translator entity

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Dunglas\ApiBundle\Annotation\Iri;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * Organization or person who adapts a creative work to different languages, regional differences and technical requirements of a target market.
 * 
 * @see http://schema.org/translator Documentation on Schema.org
 * 
 * @ORM\Entity
 * @Iri("http://schema.org/translator")
 */
class translator extends Thing
{
    /**
     * @var int
     * 
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
    /**
     * @var language
     * 
     * @ORM\ManyToMany(targetEntity="language")
     * @ORM\JoinTable(inverseJoinColumns={@ORM\JoinColumn(unique=true)})
     */
    private $languages;
    /**
     * @var Rating
     * 
     * @ORM\OneToOne(targetEntity="Rating")
     */
    private $writingExpertise;
    /**
     * @var Rating
     * 
     * @ORM\OneToOne(targetEntity="Rating")
     */
    private $expertiseSpokenLanguage;
    /**
     * @var Rating
     * 
     * @ORM\OneToOne(targetEntity="Rating")
     */
    private $levelOfAdaptation;
    /**
     * @var Person
     * 
     * @ORM\OneToOne(targetEntity="Person")
     * @ORM\JoinColumn(nullable=true)
     */
    private $employee;

    /**
     * Sets id.
     * 
     * @param int $id
     * 
     * @return $this
     */
    public function setId($id)
    {
        $this->id = $id;

        return $this;
    }

    /**
     * Gets id.
     * 
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Sets languages.
     * 
     * @param language $languages
     * 
     * @return $this
     */
    public function setLanguages(language $languages = null)
    {
        $this->languages = $languages;

        return $this;
    }

    /**
     * Gets languages.
     * 
     * @return language
     */
    public function getLanguages()
    {
        return $this->languages;
    }

    /**
     * Sets writingExpertise.
     * 
     * @param Rating $writingExpertise
     * 
     * @return $this
     */
    public function setWritingExpertise(Rating $writingExpertise = null)
    {
        $this->writingExpertise = $writingExpertise;

        return $this;
    }

    /**
     * Gets writingExpertise.
     * 
     * @return Rating
     */
    public function getWritingExpertise()
    {
        return $this->writingExpertise;
    }

    /**
     * Sets expertiseSpokenLanguage.
     * 
     * @param Rating $expertiseSpokenLanguage
     * 
     * @return $this
     */
    public function setExpertiseSpokenLanguage(Rating $expertiseSpokenLanguage = null)
    {
        $this->expertiseSpokenLanguage = $expertiseSpokenLanguage;

        return $this;
    }

    /**
     * Gets expertiseSpokenLanguage.
     * 
     * @return Rating
     */
    public function getExpertiseSpokenLanguage()
    {
        return $this->expertiseSpokenLanguage;
    }

    /**
     * Sets levelOfAdaptation.
     * 
     * @param Rating $levelOfAdaptation
     * 
     * @return $this
     */
    public function setLevelOfAdaptation(Rating $levelOfAdaptation = null)
    {
        $this->levelOfAdaptation = $levelOfAdaptation;

        return $this;
    }

    /**
     * Gets levelOfAdaptation.
     * 
     * @return Rating
     */
    public function getLevelOfAdaptation()
    {
        return $this->levelOfAdaptation;
    }

    /**
     * Sets employee.
     * 
     * @param Person $employee
     * 
     * @return $this
     */
    public function setEmployee(Person $employee)
    {
        $this->employee = $employee;

        return $this;
    }

    /**
     * Gets employee.
     * 
     * @return Person
     */
    public function getEmployee()
    {
        return $this->employee;
    }
}

and language entity

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Dunglas\ApiBundle\Annotation\Iri;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * A sub property of instrument. The language used on this action.
 * 
 * @see http://schema.org/language Documentation on Schema.org
 * 
 * @ORM\Entity
 * @Iri("http://schema.org/language")
 */
class language extends Thing
{
    /**
     * @var int
     * 
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
    /**
     * @var string
     * 
     * @ORM\Column
     * @Assert\Type(type="string")
     * @Assert\NotNull
     */
    private $language;

    /**
     * Sets id.
     * 
     * @param int $id
     * 
     * @return $this
     */
    public function setId($id)
    {
        $this->id = $id;

        return $this;
    }

    /**
     * Gets id.
     * 
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Sets language.
     * 
     * @param string $language
     * 
     * @return $this
     */
    public function setLanguage($language)
    {
        $this->language = $language;

        return $this;
    }

    /**
     * Gets language.
     * 
     * @return string
     */
    public function getLanguage()
    {
        return $this->language;
    }
}

and for api-platform version, this is an excerpt of what is in composer.json

"require": {
        "php": ">=5.5.0",
        "symfony/symfony": "~2.7",
        "doctrine/orm": "~2.2,>=2.2.3,<2.5",
        "doctrine/dbal": "<2.5",
        "doctrine/doctrine-bundle": "~1.4",
        "twig/extensions": "~1.0",
        "symfony/swiftmailer-bundle": "~2.3",
        "symfony/monolog-bundle": "~2.4",
        "sensio/distribution-bundle": "~3.0,>=3.0.12",
        "sensio/framework-extra-bundle": "~3.0,>=3.0.2",
        "incenteev/composer-parameter-handler": "~2.0",
        "dunglas/api-bundle": "~1.0",
        "nelmio/cors-bundle": "~1.4",
        "nelmio/api-doc-bundle": "~2.9@dev",
        "friendsofsymfony/http-cache-bundle": "~1.0",
        "friendsofsymfony/user-bundle": "~2.0@dev",
        "lexik/jwt-authentication-bundle": "^1.5"
    },
    "require-dev": {
        "sensio/generator-bundle": "~2.3",
        "api-platform/schema-generator": "~1.0",
        "behat/behat": "~3.0",
        "behat/symfony2-extension": "~2.0",
        "behat/mink": "~1.5",
        "behat/mink-extension": "~2.0",
        "behat/mink-browserkit-driver": "~1.1",
        "behatch/contexts": "dev-master",
        "hautelook/alice-bundle": "~0.2"
    },

Maybe :
PUT /translators/2
{ "languages": ["/languages/1", "/languages/2"] }

It gives me an error

{
  "@context": "/contexts/Error",
  "@type": "Error",
  "hydra:title": "An error occurred",
  "hydra:description": "Catchable Fatal Error: Argument 1 passed to AppBundle\\Entity\\translator::setLanguages() must be an instance of AppBundle\\Entity\\language, array given, called in /var/www/sonoclips-backend/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php on line 410 and defined",
  "trace": [
    {
      "file": "/var/www/sonoclips-backend/src/AppBundle/Entity/translator.php",
      "line": 91,
      "function": "handleError",
      "class": "Symfony\\Component\\Debug\\ErrorHandler",
      "type": "->",
      "args": [
        4096,
        "Argument 1 passed to AppBundle\\Entity\\translator::setLanguages() must be an instance of AppBundle\\Entity\\language, array given, called in /var/www/sonoclips-backend/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php on line 410 and defined",
        "/var/www/sonoclips-backend/src/AppBundle/Entity/translator.php",
        91,
        {
          "this": {}
        }
      ]
    },
    {
      "file": "/var/www/sonoclips-backend/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php",
      "line": 410,
      "function": "setLanguages",
      "class": "AppBundle\\Entity\\translator",
      "type": "->",
      "args": [
        [
          {
            "__initializer__": {},
            "__cloner__": {},
            "__isInitialized__": false
          },
          {
            "__initializer__": {},
            "__cloner__": {},
            "__isInitialized__": false
          }
        ]
      ]
    },
    {
      "file": "/var/www/sonoclips-backend/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php",
      "line": 88,
      "function": "writeProperty",
      "class": "Symfony\\Component\\PropertyAccess\\PropertyAccessor",
      "type": "->",
      "args": [
        {},
        "languages",
        [
          {
            "__initializer__": {},
            "__cloner__": {},
            "__isInitialized__": false
          },
          {
            "__initializer__": {},
            "__cloner__": {},
            "__isInitialized__": false
          }
        ]
      ]
    },
    {
      "file": "/var/www/sonoclips-backend/vendor/dunglas/api-bundle/JsonLd/Serializer/ItemNormalizer.php",
      "line": 388,
      "function": "setValue",
      "class": "Symfony\\Component\\PropertyAccess\\PropertyAccessor",
      "type": "->",
      "args": [
        {},
        "languages",
        [
          {
            "__initializer__": {},
            "__cloner__": {},
            "__isInitialized__": false
          },
          {
            "__initializer__": {},
            "__cloner__": {},
            "__isInitialized__": false
          }
        ]
      ]
    },
    {
      "file": "/var/www/sonoclips-backend/vendor/dunglas/api-bundle/JsonLd/Serializer/ItemNormalizer.php",
      "line": 258,
      "function": "setValue",
      "class": "Dunglas\\ApiBundle\\JsonLd\\Serializer\\ItemNormalizer",
      "type": "->",
      "args": [
        {},
        "languages",
        [
          {
            "__initializer__": {},
            "__cloner__": {},
            "__isInitialized__": false
          },
          {
            "__initializer__": {},
            "__cloner__": {},
            "__isInitialized__": false
          }
        ]
      ]
    },
    {
      "file": "/var/www/sonoclips-backend/vendor/symfony/symfony/src/Symfony/Component/Serializer/Serializer.php",
      "line": 290,
      "function": "denormalize",
      "class": "Dunglas\\ApiBundle\\JsonLd\\Serializer\\ItemNormalizer",
      "type": "->",
      "args": [
        {
          "languages": [
            "/languages/1",
            "/languages/2"
          ]
        },
        "AppBundle\\Entity\\translator",
        "json-ld",
        {
          "resource": {},
          "object_to_populate": {}
        }
      ]
    },
    {
      "file": "/var/www/sonoclips-backend/vendor/symfony/symfony/src/Symfony/Component/Serializer/Serializer.php",
      "line": 159,
      "function": "denormalizeObject",
      "class": "Symfony\\Component\\Serializer\\Serializer",
      "type": "->",
      "args": [
        {
          "languages": [
            "/languages/1",
            "/languages/2"
          ]
        },
        "AppBundle\\Entity\\translator",
        "json-ld",
        {
          "resource": {},
          "object_to_populate": {}
        }
      ]
    },
    {
      "file": "/var/www/sonoclips-backend/vendor/symfony/symfony/src/Symfony/Component/Serializer/Serializer.php",
      "line": 114,
      "function": "denormalize",
      "class": "Symfony\\Component\\Serializer\\Serializer",
      "type": "->",
      "args": [
        {
          "languages": [
            "/languages/1",
            "/languages/2"
          ]
        },
        "AppBundle\\Entity\\translator",
        "json-ld",
        {
          "resource": {},
          "object_to_populate": {}
        }
      ]
    },
    {
      "file": "/var/www/sonoclips-backend/vendor/dunglas/api-bundle/Controller/ResourceController.php",
      "line": 247,
      "function": "deserialize",
      "class": "Symfony\\Component\\Serializer\\Serializer",
      "type": "->",
      "args": [
        "{ \"languages\": [\"/languages/1\", \"/languages/2\"] } ",
        "AppBundle\\Entity\\translator",
        "json-ld",
        {
          "resource": {},
          "object_to_populate": {}
        }
      ]
    },
    {
      "function": "putAction",
      "class": "Dunglas\\ApiBundle\\Controller\\ResourceController",
      "type": "->",
      "args": [
        {
          "attributes": {},
          "request": {},
          "query": {},
          "server": {},
          "files": {},
          "cookies": {},
          "headers": {}
        },
        "2"
      ]
    },
    {
      "file": "/var/www/sonoclips-backend/app/bootstrap.php.cache",
      "line": 3109,
      "function": "call_user_func_array",
      "args": [
        [
          {},
          "putAction"
        ],
        [
          {
            "attributes": {},
            "request": {},
            "query": {},
            "server": {},
            "files": {},
            "cookies": {},
            "headers": {}
          },
          "2"
        ]
      ]
    },
    {
      "file": "/var/www/sonoclips-backend/app/bootstrap.php.cache",
      "line": 3071,
      "function": "handleRaw",
      "class": "Symfony\\Component\\HttpKernel\\HttpKernel",
      "type": "->",
      "args": [
        {
          "attributes": {},
          "request": {},
          "query": {},
          "server": {},
          "files": {},
          "cookies": {},
          "headers": {}
        },
        1
      ]
    },
    {
      "file": "/var/www/sonoclips-backend/app/bootstrap.php.cache",
      "line": 3222,
      "function": "handle",
      "class": "Symfony\\Component\\HttpKernel\\HttpKernel",
      "type": "->",
      "args": [
        {
          "attributes": {},
          "request": {},
          "query": {},
          "server": {},
          "files": {},
          "cookies": {},
          "headers": {}
        },
        1,
        true
      ]
    },
    {
      "file": "/var/www/sonoclips-backend/app/bootstrap.php.cache",
      "line": 2444,
      "function": "handle",
      "class": "Symfony\\Component\\HttpKernel\\DependencyInjection\\ContainerAwareHttpKernel",
      "type": "->",
      "args": [
        {
          "attributes": {},
          "request": {},
          "query": {},
          "server": {},
          "files": {},
          "cookies": {},
          "headers": {}
        },
        1,
        true
      ]
    },
    {
      "file": "/var/www/sonoclips-backend/web/app_dev.php",
      "line": 28,
      "function": "handle",
      "class": "Symfony\\Component\\HttpKernel\\Kernel",
      "type": "->",
      "args": [
        {
          "attributes": {},
          "request": {},
          "query": {},
          "server": {},
          "files": {},
          "cookies": {},
          "headers": {}
        }
      ]
    },
    {
      "file": "/var/www/sonoclips-backend/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Resources/config/router_dev.php",
      "line": 36,
      "args": [
        "/var/www/sonoclips-backend/web/app_dev.php"
      ],
      "function": "require"
    }
  ]
}

@Fercho191 Do you understand the error message ?

It means that you are trying to pass an array and you have an object in the set/get.

Did you generate the get/set by hand or the schema-generator did it ?

if I use the schema-generator, but I have confused supported cardinalities, I'm changing it to assess the outcome

I've solved! I detected the error in the setLanguages giving schema-generator

/**
     * Sets languages.
     * 
     * @param language $languages
     * 
     * @return $this
     */
    public function setLanguages(Language $languages = null)
    {
        $this->languages = $languages;

        return $this;
    }

The function receives an instance of Language, but send POST is an array, so you change the argument that it was not an instance

/**
     * Sets languages.
     * 
     * @param language $languages
     * 
     * @return $this
     */
    public function setLanguages($languages = null)
    {
        $this->languages = $languages;

        return $this;
    }

and works!

solucion

Thank @Simperfit , your comment much helped me

I'm glad that it helped you !

Can you close this ?

Try to use api-platformV2 too, @dunglas released the first alpha :)

Was this page helpful?
0 / 5 - 0 ratings