gpt4 book ai didi

php - symfony - Controller 中的查询构建器

转载 作者:行者123 更新时间:2023-11-29 11:48:04 25 4
gpt4 key购买 nike

我在 Controller 中使用查询构建器来接收数据并以 json 形式输出:

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

$qb = $qb->createQueryBuilder();

$qb->select('c')
->from('AppBundle:Customer\Customer', 'c');

$data = $qb->getQuery()->getResult();

$response = new JsonResponse();
$response->setData(
[
'data' => $data
]
);

return $response;

我的实体位于命名空间中:

namespace AppBundle\Entity\Customer;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;


/**
* @ORM\Table(name="customer")
* @ORM\Entity(repositoryClass="AppBundle\Entity\Customer\Repository")
*/
class Customer {

....

表中保存了两条记录。

当我加载网址时,它只打印两个空数组并且没有数据。

谁能解释一下为什么吗?

最佳答案

试试这个然后解压......

$response = new JsonResponse(); 
$response->setData(array( 'data' => $data ));

关于php - symfony - Controller 中的查询构建器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34571770/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com