简体   繁体   中英

How To Persist Data via API JSON SYMFONY 3

Ok so i have one entity called : Commande . with attributs : Titre , Auteur , Categorie , Editeur, Prix.

I want to create an API REST json in symfony 3 who allow any device (Android for example) to persist datas in this Entity .

for example , In my RestController , this code Allow me to recover datas from another Enity : Livres

<?php

namespace BiblioBundle\Controller;

use FOS\RestBundle\Controller\Annotations\View;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;

class BiblioRestController extends Controller
{
public function getListeAction(){
$Livres = $this->getDoctrine()->getRepository('BiblioBundle:Livres')->findAll();
if(!$Livres){
  throw $this->createNotFoundException();
}
return $Livres;
}
}

The API adresse is : http://127.0.0.1:8000/api/liste.json

How to do it ? Thx .

Commande.php :

<?php

namespace BiblioBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation\ExclusionPolicy;
use JMS\Serializer\Annotation\Expose;
use JMS\Serializer\Annotation\Groups;
use JMS\Serializer\Annotation\VirtualProperty;
use JMS\Serializer\Annotation\Type;

/**
* Commande
* @ORM\Table(name="commande")
* @ORM\Entity(repositoryClass="BiblioBundle\Repository\CommandeRepository")
* 
* @ExclusionPolicy("all")
*/
class Commande
{
/**
 * @var int
 * @ORM\Column(name="Id", type="integer")
 */
private $id;

/**
 * @var string
 * @ORM\Column(name="Titre", type="string", length=80)
 * @Expose
 */
private $titre;

/**
 * @var string
 * @ORM\Column(name="Auteur", type="string", length=50)
 * @Expose
 */
private $auteur;

/**
 * @var string
 * @ORM\Column(name="Categorie", type="string", length=60)
 * @Expose
 */
private $categorie;

/**
 * @var string
 * @ORM\Column(name="Editeur", type="string", length=70)
 * @Expose
 */
private $editeur;

/**
 * @var string
 * @ORM\Column(name="Prix", type="string", length=30)
 * @Expose
 */
private $prix;


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

/**
 * Set titre
 *
 * @param string $titre
 *
 * @return Commande
 */
public function setTitre($titre)
{
    $this->titre = $titre;

    return $this;
}

/**
 * Get titre
 *
 * @return string
 */
public function getTitre()
{
    return $this->titre;
}

/**
 * Set auteur
 *
 * @param string $auteur
 *
 * @return Commande
 */
public function setAuteur($auteur)
{
    $this->auteur = $auteur;

    return $this;
}

/**
 * Get auteur
 *
 * @return string
 */
public function getAuteur()
{
    return $this->auteur;
}

/**
 * Set categorie
 *
 * @param string $categorie
 *
 * @return Commande
 */
public function setCategorie($categorie)
{
    $this->categorie = $categorie;

    return $this;
}

/**
 * Get categorie
 *
 * @return string
 */
public function getCategorie()
{
    return $this->categorie;
}

/**
 * Set editeur
 *
 * @param string $editeur
 *
 * @return Commande
 */
public function setEditeur($editeur)
{
    $this->editeur = $editeur;

    return $this;
}

/**
 * Get editeur
 *
 * @return string
 */
public function getEditeur()
{
    return $this->editeur;
}

/**
 * Set prix
 *
 * @param string $prix
 *
 * @return Commande
 */
public function setPrix($prix)
{
    $this->prix = $prix;

    return $this;
}

/**
 * Get prix
 *
 * @return string
 */
public function getPrix()
{
    return $this->prix;
}
}

Everything is in the docs

So what you're looking for is something like

$commande = new Commande();
$commande->setAuthor('john doe');
//... use the rest of your setters as needed
$em = $this->getDoctrine()->getManager();
$em->persist($commande);
$em->flush();
return new Response('Saved new commande with id '.$commande->getId());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM