Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Symfony中按Id访问项目?_Php_Symfony - Fatal编程技术网

Php 在Symfony中按Id访问项目?

Php 在Symfony中按Id访问项目?,php,symfony,Php,Symfony,我在Symfony3中有一个控制器。所有数据的返回方式如下: [ { "id": 13, "name": "testing", "entry_date": { "date": "2017-12-20 15:23:59.000000", "timezone_type": 3, "timezone": "Europe/London" }, "last_update": { "date": "2

我在Symfony3中有一个控制器。所有数据的返回方式如下:

  [
{
    "id": 13,
    "name": "testing",
    "entry_date": {
        "date": "2017-12-20 15:23:59.000000",
        "timezone_type": 3,
        "timezone": "Europe/London"
    },
    "last_update": {
        "date": "2017-12-20 15:23:59.000000",
        "timezone_type": 3,
        "timezone": "Europe/London"
    }
},
{
    "id": 30,
    "name": "testing2",
    "entry_date": {
        "date": "2017-12-20 22:02:37.000000",
        "timezone_type": 3,
        "timezone": "Europe/London"
    },
    "last_update": {
        "date": "2017-12-20 22:02:37.000000",
        "timezone_type": 3,
        "timezone": "Europe/London"
    }
}
]
 /**
 * @Method("GET")
 * @Route("/item/{item_id}")
 * @View()
 * @ApiDoc(
 *   resource = true,
 *   description = "Get an item record",
 *   section = "Spark DTR",
 *   )
 */
public function getItem($item_id)
{
  $em = $this->getDoctrine()->getManager('app');
  $mi_repo = $em->getRepository('AppBundle:Item')->find($item_id);

  if(empty($mi_repo)) {
      return new JsonResponse("Invalid Item ID", 404);
  }

  return new JsonResponse($mi_repo, 200);
}
我正在尝试按其id返回单个项目。到目前为止,我的方法如下所示:

  [
{
    "id": 13,
    "name": "testing",
    "entry_date": {
        "date": "2017-12-20 15:23:59.000000",
        "timezone_type": 3,
        "timezone": "Europe/London"
    },
    "last_update": {
        "date": "2017-12-20 15:23:59.000000",
        "timezone_type": 3,
        "timezone": "Europe/London"
    }
},
{
    "id": 30,
    "name": "testing2",
    "entry_date": {
        "date": "2017-12-20 22:02:37.000000",
        "timezone_type": 3,
        "timezone": "Europe/London"
    },
    "last_update": {
        "date": "2017-12-20 22:02:37.000000",
        "timezone_type": 3,
        "timezone": "Europe/London"
    }
}
]
 /**
 * @Method("GET")
 * @Route("/item/{item_id}")
 * @View()
 * @ApiDoc(
 *   resource = true,
 *   description = "Get an item record",
 *   section = "Spark DTR",
 *   )
 */
public function getItem($item_id)
{
  $em = $this->getDoctrine()->getManager('app');
  $mi_repo = $em->getRepository('AppBundle:Item')->find($item_id);

  if(empty($mi_repo)) {
      return new JsonResponse("Invalid Item ID", 404);
  }

  return new JsonResponse($mi_repo, 200);
}
但是,此方法当前返回“无效项ID”(如果没有项),或者

{} 
如果有项目!我想返回项目的内容。感谢您的帮助,

$em->getRepository('AppBundle:Item')->find($item_id);
结果是得到一个对象,JsonResponse()需要一个数组作为参数

这里有两个选项,您可以安装serializer组件()并将对象直接序列化为Json并返回

$jsonContent = $serializer->serialize($mi_repo, 'json');
或者,如果不想设置序列化程序,也可以使用带getArrayResult()的条令查询生成器返回数组而不是对象

$query = $em->createQueryBuilder()
        ->select('p')
        ->from('Products', 'p')
        ->where('p.id= :item_id')
        ->setParameter('id', $item_id)
        ->getQuery();
$mi_repo = $query->getArrayResult();

希望这有帮助!

试试这个:$mi_repo=$this->getDoctrine()->getRepository('AppBundle:Item')->find($Item_id);