Kane Samba
Kane Samba

Reputation: 279

FindAll() is not working , Symfony 3 FosrestBundle API

So I made an api with FOSRestBundle who find and return datas by the id

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

The result : Result

Now i want to use the method findAll() , to return all the data from the repository by changing like this :

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

But it ain't working . 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 entity with the JMS Serializer

<?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...

How to fix this please , thanks .

Upvotes: 0

Views: 620

Answers (1)

Miro
Miro

Reputation: 1899

findAll() method is working correctly, but it doesn't return object, but an array of objects, thats why you get 404, because of your following check.

Change your code:

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

to this one:

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

Upvotes: 0

Related Questions