这是一个新手问题,但我无法在任何地方找到答案。
我正在尝试从 Symfony 3.4 中的控制器返回 JSON 响应。
在控制器中我有:
namespace MegBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Routing\Annotation\Route;
use MegBundle\Entity\message;
use Doctrine\ORM\Tools\Pagination\Paginator;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\Common\Inflector\Inflector;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
class MegController extends Controller
{
/**
* @Route("/message/board/page/{page}", name = "message_board", defaults = {"page" = 1}, requirements = {"page" = "\d+"})
* @Method("GET")
*/
public function showMeg(Request $request, $page)
{
$entityManager = $this->getDoctrine()->getEntityManager();
if ($request->query->get("DESC") != null) {
$orderById = "DESC=ID";
$orderby = 'DESC';
}
if ($request->query->get("DESC") == null) {
$orderById = "ASC=ID";
$orderby = 'ASC';
}
$per = 5;
$start = ($page-1)*$per;
$query = $entityManager->createQueryBuilder()
->select("r")
->from("MegBundle:message", "r")
->orderBy('r.id', $orderby)
->setFirstResult($start)
->setMaxResults($per);
}
但我明白了
调用数组上的成员函数 getName()。
我希望我能像这样得到 JosnResponse
{"result":"ok","ret":{"name":[{"name":"AAA","mes":"1234","update_time":"2019-09-20 10:54: 50"},{"name":"AAA","mes":"1234","update_time":"2019-09-20 11:08:53"},{"name":"赖","mes ":"AppleAppleappleAPPLE","update_time":"2019-09-20 11:11:09"}]}
慕勒3428872