簡體   English   中英

FindAll()無法正常工作,Symfony 3 FosrestBundle API

[英]FindAll() is not working , Symfony 3 FosrestBundle API

所以我用FOSRestBundle制作了一個API,該API通過ID查找並返回數據

   public function getIntAction($int){
$Interventions = $this->getDoctrine()->getRepository('CBMedBundle:Interventions')->find($int);
if(!is_object($Interventions)){
  throw $this->createNotFoundException();
}
return $Interventions;
}

結果: 結果

現在,我想使用方法findAll(),通過像這樣更改從存儲庫返回所有數據:

  public function getIntAction(){
$Interventions = $this->getDoctrine()->getRepository('CBMedBundle:Interventions')->findAll();
if(!is_object($Interventions)){
  throw $this->createNotFoundException();
}
return $Interventions;
}

但這沒用。 錯誤圖片

{
"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序列化程序的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...

謝謝,如何解決這個問題。

findAll()方法可以正常工作,但是它不返回對象,而是返回對象數組,這就是為什么要進行404檢查的原因。

更改您的代碼:

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

對此:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM