简体   繁体   English

Symfony主义返回空对象

[英]Symfony Doctrine Returns Empty Object

I'm trying to return a specific row in my table using Doctrine in conjunction with Symfony 2 and Vue 2. Here is the code I'm using to achieve this. 我正在尝试使用Doctrine结合Symfony 2和Vue 2返回表中的特定行。这是我用来实现此目的的代码。

/**
 * Get all children of the administrator
 * @return JsonResponse
 */
public function getChildrenAction() {

$em = $this->getDoctrine()->getManager();

$records = $em->getRepository('TestUserBundle:NameOfEntity')-> 
findOneBy(array('id' => 123456));

return new JsonResponse($records);

}

My problem is that on the front-end, I always end up with an empty object such as {}, and can't do anything with that. 我的问题是,在前端,我总是以空对象(例如{})结尾,而对此无能为力。 Now I know this is returning the instance of that User, but I want all the data associated with that user in the table row on the front-end, so that I can use it to show specific user details. 现在,我知道这将返回该用户的实例,但是我希望在前端的表行中与该用户关联的所有数据,以便可以使用它显示特定的用户详细信息。 I know how to pass specific parts of the object, such as Id by tacking on "->id" or "->getId()", but have no clue how to get the whole object. 我知道如何传递对象的特定部分,例如通过附加“-> id”或“-> getId()”来标识,但不知道如何获取整个对象。 Any help with this, or a gentle shove in the right direction would be greatly appreciated! 任何与此有关的帮助,或在正确方向轻轻推一下,将不胜感激!

FindOneBy method returns a entity object according with the criteria given, if you want get that data from your view (twig template) 如果要从视图中获取数据,则FindOneBy方法将根据给定的条件返回实体对象(树枝模板)

function doAction(){

  //...
  $this->render('some_template.html.twig', ['record' => $record ]);
}

some_template.html.twig some_template.html.twig

{{ dump(record) }}

Or if you wish return a response without a template, you could use JsonResponse class. 或者,如果您希望返回不带模板的响应,则可以使用JsonResponse类。

return new JsonResponse($records);

The JsonResponse class sets the Content-Type header to application/json and encodes your data to JSON when needed. JsonResponse类将Content-Type标头设置为application / json,并在需要时将数据编码为JSON。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM