Symfony中返回了意外的集合

时间:2019-02-22 13:56:56

标签: symfony doctrine-orm

我创建了一个与合作伙伴具有ManyToOne关系的客户实体。合作伙伴与客户的OneToMany关系相反。

我正在打电话给$partner->getCustomers(),尽管我无法解释某些内容,但我确实得到了可以的结果。首先,这是查询:

public function getCustomersByPartner(SerializerInterface $serializer, $id)
{
    /* @var $partner Partner */
    $partner = $this->getDoctrine()->getRepository(Partner::class)->find($id);
    $customers = $serializer->serialize($partner->getCustomers(), 'json');

    $response = new Response($customers);
    $response->headers->set('Content-Type', 'application/json');

    return $response;
}

这是完整的结果(调用host / partner / 3路由时,其中3是伙伴ID):

[
    {
        "id": 4,
        "url": "www.test.com",
        "subscription_status": "active",
        "first_payment": "2019-02-22T09:55:47+00:00",
        "created_at": "2019-02-22T09:55:47+00:00",
        "updated_at": "2019-02-22T09:55:47+00:00",
        "partner": {
            "id": 3,
            "firstname": "Partner3",
            "lastname": "Partner3",
            "authenticated": 1,
            "created_at": "2019-02-22T10:05:04+00:00",
            "updated_at": "2019-02-22T10:05:04+00:00",
            "customers": [
                {
                    "id": 9,
                    "url": "www.blabla.com",
                    "subscription_status": "active",
                    "first_payment": "2019-02-22T13:23:43+00:00",
                    "created_at": "2019-02-22T13:23:43+00:00",
                    "updated_at": "2019-02-22T13:23:43+00:00"
                }
            ]
        }
    },
    {
        "id": 9,
        "url": "www.blabla.com",
        "subscription_status": "active",
        "first_payment": "2019-02-22T13:23:43+00:00",
        "created_at": "2019-02-22T13:23:43+00:00",
        "updated_at": "2019-02-22T13:23:43+00:00",
        "partner": {
            "id": 3,
            "firstname": "Partner3",
            "lastname": "Partner3",
            "authenticated": 1,
            "created_at": "2019-02-22T10:05:04+00:00",
            "updated_at": "2019-02-22T10:05:04+00:00",
            "customers": [
                {
                    "id": 4,
                    "url": "www.test.com",
                    "subscription_status": "active",
                    "first_payment": "2019-02-22T09:55:47+00:00",
                    "created_at": "2019-02-22T09:55:47+00:00",
                    "updated_at": "2019-02-22T09:55:47+00:00"
                }
            ]
        }
    }
]

我确实拥有属于合作伙伴ID 3 的所有客户,但是我希望我的customers数组(在两个“合作伙伴”对象中)都包含与该合作伙伴关联的每个客户(因此,在这种情况下,客户4和客户9)。

我实在没话可说,希望您理解我的意思。 那就是我所期望的:

"customers": [
                {
                    "id": 9,
                    "url": "www.blabla.com",
                    "subscription_status": "active",
                    "first_payment": "2019-02-22T13:23:43+00:00",
                    "created_at": "2019-02-22T13:23:43+00:00",
                    "updated_at": "2019-02-22T13:23:43+00:00"
                },
                {
                    "id": 4,
                    "url": "www.test.com",
                    "subscription_status": "active",
                    "first_payment": "2019-02-22T09:55:47+00:00",
                    "created_at": "2019-02-22T09:55:47+00:00",
                    "updated_at": "2019-02-22T09:55:47+00:00"
                }
            ]

是因为Symfony首先重访了第一个客户,然后显示了剩余的内容吗?这是一种特定于教义的行为吗?

有趣的是,如果我做相反的事情,然后去看看哪个客户属于哪个合作伙伴,我确实得到了返回的结果: (呼叫主机/客户/ 4路由,其中​​4是客户ID)

{
    "id": 3,
    "firstname": "Partner3",
    "lastname": "Partner3",
    "authenticated": 1,
    "created_at": "2019-02-22T10:05:04+00:00",
    "updated_at": "2019-02-22T10:05:04+00:00",
    "customers": [
        {
            "id": 4,
            "url": "www.test.com",
            "subscription_status": "active",
            "first_payment": "2019-02-22T09:55:47+00:00",
            "created_at": "2019-02-22T09:55:47+00:00",
            "updated_at": "2019-02-22T09:55:47+00:00"
        },
        {
            "id": 9,
            "url": "www.blabla.com",
            "subscription_status": "active",
            "first_payment": "2019-02-22T13:23:43+00:00",
            "created_at": "2019-02-22T13:23:43+00:00",
            "updated_at": "2019-02-22T13:23:43+00:00"
        },
        {
            "id": 10,
            "url": "www.blabla.com",
            "subscription_status": "active",
            "first_payment": "2019-02-22T13:53:02+00:00",
            "created_at": "2019-02-22T13:53:02+00:00",
            "updated_at": "2019-02-22T13:53:02+00:00"
        }
    ]
}

多数民众赞成在功能:

/**
 * @Route("/customer/{id}", name="customer_detail")
 * @param SerializerInterface $serializer Serializer service
 * @param Integer $id Id of customer
 * @return Response Return name of the partner
 */
public function getPartnerByCustomer(SerializerInterface $serializer, $id)
{
    /* @var $customer Customer */
    $customer = $this->getDoctrine()->getRepository(Customer::class)->find($id);
    $partner = $serializer->serialize($customer->getPartner(), 'json');

    $response = new Response($partner);
    $response->headers->set('Content-Type', 'application/json');

    return $response;
}

这正是我对第一个请求的期望。谢谢您的宝贵时间。

0 个答案:

没有答案