FindAll() 不工作,Symfony 3 FosrestBundle API
FindAll() is not working , Symfony 3 FosrestBundle API
所以我用 FOSRestBundle 创建了一个 api,它通过 id
找到和 return 数据
public function getIntAction($int){
$Interventions = $this->getDoctrine()->getRepository('CBMedBundle:Interventions')->find($int);
if(!is_object($Interventions)){
throw $this->createNotFoundException();
}
return $Interventions;
}
结果:
Result
现在我想使用方法 findAll() 来 return 存储库中的所有数据,方法如下:
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"
]
Interventions.php 带有 JMS 序列化程序的实体
<?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...
请问如何解决这个问题,谢谢。
findAll()
方法工作正常,但它不是 return 对象,而是一个对象数组,这就是你得到 404 的原因,因为你进行了以下检查。
更改您的代码:
if(!is_object($Interventions)){
throw $this->createNotFoundException();
}
给这个:
if (!$Interventions) {
throw $this->createNotFoundException();
}
所以我用 FOSRestBundle 创建了一个 api,它通过 id
找到和 return 数据 public function getIntAction($int){
$Interventions = $this->getDoctrine()->getRepository('CBMedBundle:Interventions')->find($int);
if(!is_object($Interventions)){
throw $this->createNotFoundException();
}
return $Interventions;
}
结果: Result
现在我想使用方法 findAll() 来 return 存储库中的所有数据,方法如下:
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"
]
Interventions.php 带有 JMS 序列化程序的实体
<?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...
请问如何解决这个问题,谢谢。
findAll()
方法工作正常,但它不是 return 对象,而是一个对象数组,这就是你得到 404 的原因,因为你进行了以下检查。
更改您的代码:
if(!is_object($Interventions)){
throw $this->createNotFoundException();
}
给这个:
if (!$Interventions) {
throw $this->createNotFoundException();
}