为什么我在 mi Symfony 2 REST API 中以某些值获取所有数据库信息?

Why I'm getting all database info in some values in mi Symfony 2 REST API?

我正在使用 Symfony 2 构建 REST API,我遇到了一个非常罕见的问题...我有一个 "customers" table 与 OneToMany 的关系另一个 "control" table 并且如果我尝试让客户的值在 "control" table 我得到的是我所有的数据库信息 JSON 格式... :S

例如:id=12 的客户在 "control" table 中没有关联数据,所以如果我有 http://localhost/Ourentec/web/app_dev.php/api/v1/customers/12 我得到了正确的 JSON

{
    "customer": {
        "id": 12,
        "name": "Zampacontos",
        "lastname": "",
        "address": "",
        "phone": "000000000",
        "pass": "no tiene",
        "tasks": "Portátil Toshiba con cargador y funda negra \"tec air\".\r\n Cambiar pila BIOS y comprobar que no de errores (optimizar) Urgente ",
        "status": "Terminado",
        "email": "",
        "date": "2015-11-06T13:06:00+0100",
        "location": "Tienda",
        "is_active": 0,
        "controls": [],
        "histories": []
    }
}

但是如果我对 id=11 做同样的事情("controls" 和 "histories" 的客户是我获得所有数据库信息的时间!!!

有什么想法吗??提前致谢:)

已编辑:

这是我的 API 一个或所有客户的控制器:

<?php

namespace Ourentec\OurentecApiBundle\Controller\v1;


use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\Controller\Annotations as Rest;
use Symfony\Component\HttpFoundation\Request;

class CustomerController extends FOSRestController{
    /**
     * @return array
     * @Rest\Get("/customers/{id}")
     * @Rest\View
     */
    public function getCustomerAction($id)
    {
        $customer = $this->getDoctrine()->getRepository('OurentecBundle:Customer')->find($id);

        return array('customer' => $customer);
    }

    /**
     *
     * @return array
     * @Rest\Get("/customers/")
     * @Rest\View
     */
    public function getUsersAction()
    {
        $customers = $this->getDoctrine()->getRepository('OurentecBundle:Customer')->findAll();

        return array('customers' => $customers);
    }
} 

如果我执行 http://localhost/Ourentec/web/app_dev.php/api/v1/customers/,我也会获得所有数据库信息。甚至 "User" table 当然还有我所有的用户、密码(加密)等等... :O

这是因为 findfindAll 方法的行为不同。

在幕后,findAll 使用预加载,但 find 是惰性的。

因此 findAll 将从所有相关实体中获取数据。

Find 只会获取关系的 ID,将代理对象放在关系字段中。

如果您想要完全控制,只需使用查询生成器在您的实体存储库中实现 findAll 方法。

嗯,所以我决定只从我的客户那里得到一些有趣的字段。

型号:

public function getCustomerForApi($customerId)
    {
        $customer = $this->em
            ->createQuery('select c.name, c.lastname, c.address, c.phone, c.pass, c.tasks, c.email, c.status, c.location
            from OurentecBundle:Customer c where c.id = :id')
            ->setParameter('id', $customerId)
            ->getResult();

        return $customer;
    }

和控制器:

/**
     * @return array
     * @Rest\Get("/customers/{id}")
     * @Rest\View
     */
    public function getCustomerAction(Request $request)
    {
        $customerModel = $this->get('ourentec.customer_model');
        $customer = $customerModel->getCustomerForApi($request->get('id'));

        return array('customer' => $customer);
    }

这不是我想要的,但它可以很好地用于测试:)

再次感谢 Mateusz!!

为什么不使用 :

$customer = $this->getDoctrine()->getRepository('OurentecBundle:Customer')->findOneById($id);