【发布时间】: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 时,它只打印两个空数组并且没有数据。
谁能解释一下原因?
【问题讨论】:
-
对象不会被自动序列化。将 getResult 替换为 getArrayResult doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/… 稍后您可能需要阅读序列化程序:symfony.com/doc/current/components/serializer.html 或 php.net/manual/en/class.jsonserializable.php
标签: php mysql symfony doctrine load