我正在开发REST API。因此我创建了我的实体,例如这个musee.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Musee
*
* @ORM\Table(name="musee")
* @ORM\Entity(repositoryClass="AppBundle\Repository\MuseeRepository")
*/
class Musee
{
/**
* @var int
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
*/
private $id;
/**
* @var string
*
* @ORM\Column(type="string", nullable=false)
*/
private $nom;
/**
* @var string
*
* @ORM\Column(type="string", nullable=false)
*/
private$adresse;
/**
* @var integer
*
* @ORM\Column(type="integer", nullable=false)
*/
private $horaireOuverture;
/**
* @var integer
*
* @ORM\Column(type="integer", nullable=false)
*/
private $horaireFermeture;
/**
* @ORM\OneToMany(targetEntity="AppBundle\Entity\Bateau", mappedBy="musee")
* @ORM\JoinColumn(referencedColumnName="id")
*/
private $bateaux;
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @param int $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* @return string
*/
public function getNom()
{
return $this->nom;
}
/**
* @param string $nom
*/
public function setNom($nom)
{
$this->nom = $nom;
}
/**
* @return string
*/
public function getAdresse()
{
return $this->adresse;
}
/**
* @param string $adresse
*/
public function setAdresse($adresse)
{
$this->adresse = $adresse;
}
/**
* @return int
*/
public function getHoraireOuverture()
{
return $this->horaireOuverture;
}
/**
* @param int $horaireOuverture
*/
public function setHoraireOuverture($horaireOuverture)
{
$this->horaireOuverture = $horaireOuverture;
}
/**
* @return int
*/
public function getHoraireFermeture()
{
return $this->horaireFermeture;
}
/**
* @param int $horaireFermeture
*/
public function setHoraireFermeture($horaireFermeture)
{
$this->horaireFermeture = $horaireFermeture;
}
/**
* @return mixed
*/
public function getBateaux()
{
return $this->bateaux;
}
/**
* @param mixed $bateaux
*/
public function setBateaux($bateaux)
{
$this->bateaux = $bateaux;
}
}
我的MuseeRepository也存在,并且在我的config.yml中(我们都安装了作曲家):
# Nelmio CORS
nelmio_cors:
defaults:
allow_credentials: false
allow_origin: ['*']
allow_headers: ['Content-Type']
allow_methods: ['POST', 'PATCH', 'GET', 'DELETE', 'OPTIONS']
max_age: 3600
paths:
'^/api':
# FosRest
fos_rest:
routing_loader:
include_format: false
view:
view_response_listener: true
format_listener:
rules:
- { path: '^/', priorities: ['json'], fallback_format: 'json' }
在我的MuseeController中,这就是我所说的:
<?php
/**
* Created by PhpStorm.
* User: Fanny
* Date: 28/03/2018
* Time: 16:13
*/
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Controller\FOSRestController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use FOS\RestBundle\View\View;
use AppBundle\Entity\Musee;
class MuseeController extends Controller
{
/**
* @Rest\View(serializerGroups={"musee"})
* @Rest\Get("/musee")
*/
// Fonction qui renvoie toutes all musee
public function findMusee()
{
$musee = $this->getDoctrine()
->getRepository('AppBundle:Musee')
->findAll();
return $musee;
}
}
我还使用了一个序列化程序文件,我想知道我的问题是不是来自那里...虽然所有这些元素都在我的数据库中,因为我用doctrine生成它。
AppBundle\Entity\Musee:
attributes:
id:
groups: ['musee']
nom:
groups: ['musee']
adresse:
groups: ['musee']
horaire_ouverture:
groups: ['musee']
horaire_fermeture:
groups: ['musee']
bateaux:
groups: ['musee']
我的问题是当我尝试调用我的localhost:8000 / musee时,与数据库中的内容相比,我获得了大量的括号,但它们显示为空。
我想我可能会错过一步,但我不确定在哪里搜索。你觉得我应该在哪里看看?
更新
我拥有的Symfony版本是3.4。 我在config.yml中启用了序列化程序,并使用composer:jms / serializer-bundle安装。 在我的应用内核中我有:
new FOS\RestBundle\FOSRestBundle(),
new JMS\SerializerBundle\JMSSerializerBundle(),
new Nelmio\CorsBundle\NelmioCorsBundle(),
这个问题对我所有的实体都很常见。我得到了正确数量的括号,但里面没有内容。
答案 0 :(得分:0)
FOSRestBundle
的文档介绍了序列化: Enable a Serializer
但CSS目前似乎正在崩溃,所以我引用它:
C)启用串行器
此捆绑包需要序列化程序才能正常工作。在大多数情况下,您需要启用序列化程序或安装序列化程序。此捆绑包尝试以下(按给定顺序)确定要使用的序列化程序:
- 您使用
fos_rest.services.serializer
配置的那个(如果您这样做)。- JMS序列化程序,如果
JMSSerializerBundle
可用(并已注册)。- 醇>
Symfony Serializer
如果启用(或任何称为序列化程序的服务)。
Symfony
?Serializer
组件?Musee
或所有实体吗?请用这些更新问题,我们可以跟进。
不禁想知道它是否与Symfony 3.4
中变为私有的所有服务有关。可能FOSRestBundle
无法找到JMSSerializerBundle
。您安装了哪个版本的FOSRestBundle
?
我发现FOSRestBundle
的所有已发布版本都指定了^3.0
依赖项要求,但上述内容特定于v3.4
。
答案 1 :(得分:0)
这是因为您返回的是实体数组而不是有效的json响应。我也在使用REST / API,但没有FOSRest,所以我无法完全确定FOSREST如何进行序列化,但最终确实如此。 解决方法可能是在控制器中返回JSONResponse,所有实体都以某种方式序列化,而不是返回您当前正在执行的实体数组
return $musee;
答案 2 :(得分:0)
我重新创建了我的项目,这次没有出错...我不确定是什么原因,但我使用了相同的Symfony版本和命令。因此,即使我试图比较两者并且没有看到任何差异,也可能在某处出现问题。如果我找到它,我会试着告诉你。