繁体   English   中英

如何通过API JSON SYMFONY 3保留数据

[英]How To Persist Data via API JSON SYMFONY 3

好吧,我有一个实体叫: Commande 带有属性:Titre,Auteur,Categorie,Editeur,Prix。

我想在symfony 3中创建一个API REST json,它允许任何设备(例如Android)将数据持久化在此Entity中。

例如,在我的RestController中,此代码允许我从另一个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;
}
}

API地址为: http://127.0.0.1:8000/api/liste.json : http://127.0.0.1:8000/api/liste.json : http://127.0.0.1:8000/api/liste.json api/liste.json

怎么做 ? 谢谢 。

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;
}
}

一切都在文档中

所以您想要的是

$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());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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