Search code examples
restsymfonyfosuserbundle

Trying to insert data ManyToOne FOSRestBundle


I'm trying to creating an API REST with the bundle FOSRestBundle (SF5). I've an entity "Categorie" which can have an parent "Categorie". Here is the entity :

<?php

namespace App\Entity\Main;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation\Expose;
use JMS\Serializer\Annotation\ExclusionPolicy;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity(repositoryClass="App\Repository\CategorieRepository")
 * @ORM\Table(name="categorie")
 * @ExclusionPolicy("all")
 */
class Categorie
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @Expose
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @var string
     * @Assert\NotBlank()
     * @Expose
     * @ORM\Column(type="string", length=255)
     */
    private $libelle;

    /**
     * @var string
     * @Assert\NotBlank()
     * @Expose
     * @ORM\Column(type="string", length=255)
     */
    private $icone;

    /**
     * @var Categorie
     * @ORM\ManyToOne(targetEntity="App\Entity\Main\Categorie", inversedBy="categories", cascade={"all"}, fetch="EAGER")
     * @ORM\JoinColumn(name="categorie_parent_id", referencedColumnName="id", nullable=true)
     */
    private $categorieParent;

    /**
     * @var ArrayCollection
     * @ORM\OneToMany(targetEntity="App\Entity\Main\Categorie", mappedBy="categorieParent")
     */
    private $categories;

    /**
     * @var ArrayCollection
     * @ORM\OneToMany(targetEntity="App\Entity\Main\Produit", mappedBy="categorie")
     */
    private $produits;

    public function __construct()
    {
        $this->produits = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getLibelle(): ?string
    {
        return $this->libelle;
    }

    public function setLibelle(string $libelle): self
    {
        $this->libelle = $libelle;

        return $this;
    }

    public function getIcone(): ?string
    {
        return $this->icone;
    }

    public function setIcone(string $icone): self
    {
        $this->icone = $icone;

        return $this;
    }

    public function setCategorieParent(Categorie $categorieParent): self
    {
        $this->categorieParent = $categorieParent;

        return $this;
    }

    public function getCategorieParent(Categorie $categorieParent)
    {
        return $this->categorieParent;
    }
}

Here is my action in controller :

/**
     * @Rest\View(statusCode=Response::HTTP_CREATED)
     * @Rest\Post("/api/{_locale}/categorie/create", name="api_categorie_create")
     * @ParamConverter("categorie", converter="fos_rest.request_body")
     * @IsGranted("ROLE_SUPER_ADMIN")
     * @return Categorie|View
     */
    public function create(Categorie $categorie, ConstraintViolationList $violations)
    {
        if (count($violations)) {
            return $this->view($violations, Response::HTTP_BAD_REQUEST);
        }

        $em = $this->getDoctrine()->getManager('main');
        $em->persist($categorie);
        $em->flush();

        return $categorie;
    }

When I use postman to insert data with this content :

{
    "libelle":"Blonde", 
    "icone":"blonde.png", 
    "categorieParent.id": 1
}

"libelle" and "icone" are inserted but "categorieParent" wasn't set. I've try :

{
    "libelle":"Blonde", 
    "icone":"blonde.png", 
    "categorieParent": 1
}
{
    "libelle":"Blonde", 
    "icone":"blonde.png", 
    "categorieParent": {
        "id": 1
    }
}

For each try, I set id with number and string.

And anything doesn't work. Thx for help :) !


Solution

  • CategorieParent will only accept a Categorie entity; libelle and icone work because they are simple strings. You should use the passed integer to fetch the Entity, then save the values.