2016-05-15 36 views
0

所以我做了FOSRestBundle的API誰找到並通過ID返回DATAS的FindAll()不工作,Symfony的3 FosrestBundle API

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

但它不起作用。與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... 

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實體如何解決這個問題,請,謝謝。

回答

0

findAll()方法工作正常,但它不返回對象,而是一個對象數組,這就是爲什麼你得到404,因爲你的下面的檢查。

更改您的代碼:

if(!is_object($Interventions)){ 
    throw $this->createNotFoundException(); 
} 

這一個:

if (!$Interventions) { 
    throw $this->createNotFoundException(); 
} 
+0

由於其工作 –