【问题标题】:symfony - querybuilder in controllersymfony - 控制器中的查询构建器
【发布时间】:2016-01-02 23:22:50
【问题描述】:

我在控制器中使用查询构建器来接收数据并以 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 {

....

表中保存了两条记录。

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

谁能解释一下原因?

【问题讨论】:

标签: php mysql symfony doctrine load


【解决方案1】:

试试这个然后解压……

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

【讨论】:

  • 这与问题中的内容有何不同?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-30
  • 1970-01-01
  • 1970-01-01
  • 2014-04-19
  • 1970-01-01
相关资源
最近更新 更多