所以我用FOSRestBundle做了一个API,用id找到并返回数据
public function getIntAction($int){
$Interventions = $this->getDoctrine()->getRepository('CBMedBundle:Interventions')->find($int);
if(!is_object($Interventions)){
throw $this->createNotFoundException();
}
return $Interventions;
}
结果: Result
现在我想使用方法findAll(),通过这样更改来返回存储库中的所有数据:
public function getIntAction(){
$Interventions = $this->getDoctrine()->getRepository('CBMedBundle:Interventions')->findAll();
if(!is_object($Interventions)){
throw $this->createNotFoundException();
}
return $Interventions;
}
但是它还没有用。 error image
{
"error": {
"code": 404,
"message": "Not Found",
"exception": [
{
"message": "Not Found",
"class": "Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException",
"trace": [
{
"namespace": "",
"short_class": "",
"class": "",
"type": "",
"function": "",
"file": "C:\\wamp\\www\\Symfony\\vendor\\symfony\\symfony\\src\\Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller.php",
"line": 252,
"args": []
},
{
"namespace": "Symfony\\Bundle\\FrameworkBundle\\Controller",
"short_class": "Controller",
"class": "Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller",
"type": "->",
"function": "createNotFoundException",
"file": "C:\\wamp\\www\\Symfony\\src\\CBMedBundle\\Controller\\CBMedRestController.php",
"line": 21,
"args": []
},
{
"namespace": "CBMedBundle\\Controller",
"short_class": "CBMedRestController",
"class": "CBMedBundle\\Controller\\CBMedRestController",
"type": "->",
"function": "getIntAction",
"file": null,
"line": null,
"args": []
},
{
"namespace": "",
"short_class": "",
"class": "",
"type": "",
"function": "call_user_func_array",
"file": "C:\\wamp\\www\\Symfony\\vendor\\symfony\\symfony\\src\\Symfony\\Component\\HttpKernel\\HttpKernel.php",
"line": 139,
"args": [
[
"array",
[
[
"object",
"CBMedBundle\\Controller\\CBMedRestController"
],
[
"string",
"getIntAction"
]
]
],
[
"array",
[]
]
]
},
{
"namespace": "Symfony\\Component\\HttpKernel",
"short_class": "HttpKernel",
"class": "Symfony\\Component\\HttpKernel\\HttpKernel",
"type": "->",
"function": "handleRaw",
"file": "C:\\wamp\\www\\Symfony\\vendor\\symfony\\symfony\\src\\Symfony\\Component\\HttpKernel\\HttpKernel.php",
"line": 62,
"args": [
[
"object",
"Symfony\\Component\\HttpFoundation\\Request"
],
[
"string",
"1"
]
]
},
{
"namespace": "Symfony\\Component\\HttpKernel",
"short_class": "HttpKernel",
"class": "Symfony\\Component\\HttpKernel\\HttpKernel",
"type": "->",
"function": "handle",
"file": "C:\\wamp\\www\\Symfony\\vendor\\symfony\\symfony\\src\\Symfony\\Component\\HttpKernel\\Kernel.php",
"line": 169,
"args": [
[
"object",
"Symfony\\Component\\HttpFoundation\\Request"
],
[
"string",
"1"
],
[
"boolean",
true
]
]
},
{
"namespace": "Symfony\\Component\\HttpKernel",
"short_class": "Kernel",
"class": "Symfony\\Component\\HttpKernel\\Kernel",
"type": "->",
"function": "handle",
"file": "C:\\wamp\\www\\Symfony\\web\\app_dev.php",
"line": 30,
"args": [
[
"object",
"Symfony\\Component\\HttpFoundation\\Request"
]
]
},
{
"namespace": "",
"short_class": "",
"class": "",
"type": "",
"function": "require",
"file": "C:\\wamp\\www\\Symfony\\vendor\\symfony\\symfony\\src\\Symfony\\Bundle\\FrameworkBundle\\Resources\\config\\router_dev.php",
"line": 40,
"args": [
[
"string",
"C:\\wamp\\www\\Symfony\\web\\app_dev.php"
]
使用JMS Serializer的Interventions.php实体
<?php
namespace CBMedBundle\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;
/**
* Interventions
*
* @ORM\Table(name="interventions")
*@ORM\Entity(repositoryClass="CBMedBundle\Repository\InterventionsRepository")
*
* @ExclusionPolicy("all")
*/
class Interventions
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var \Date
*
* @ORM\Column(name="Date", type="date")
* @Expose
*/
private $date;
/**
* @var string
*
* @ORM\Column(name="Salle", type="string", length=20)
* @Expose
*/
private $salle;
/**
* @var string
*
* @ORM\Column(name="TypeInter", type="string", length=32)
* @Expose
*/
private $typeInter;
/**
* @var string
*
* @ORM\Column(name="TypeAnesthesie", type="string", length=50)
* @Expose
*/
private $typeAnesthesie;
/**
* @var string
*
* @ORM\Column(name="TrancheInter", type="string", length=50)
* @Expose
*/
private $TrancheInter;
etc...
如何解决这个问题,谢谢。
答案 0 :(得分:0)
findAll()
方法工作正常,但它不会返回对象,而是一个对象数组,这就是为什么你得到404的原因,因为你有以下检查。
更改您的代码:
if(!is_object($Interventions)){
throw $this->createNotFoundException();
}
到这一个:
if (!$Interventions) {
throw $this->createNotFoundException();
}