推动多对多加入

问题描述 投票:0回答:1

也许我对此很愚蠢,但我想使用 propel 从数据库中获取多对多结果集。

我有一个 Acl_Customer、Acl_Group 和一个 Acl_Customer_Group 表。最后一个是客户和团体之间的交叉引用。

现在,我只想能够通过组表上的联接来获取我的 acl 客户。

     $customers = CustomerQuery::create()
         ->joinCustomerGroup()
         ->paginate($page, $limit);

     return $customers->getResults()->getData();

这给我带来以下几点:

[{"id":1,"username":"foo","email":"[email protected]"}]

但我需要这样的东西:

[{"id":1,"username":"foo","email":"[email protected]","groups":[{"name":"developer"}, ...]}]

有人有想法吗?

php symfony join many-to-many propel
1个回答
1
投票

您无法在 SQL 中“连接”n 对 n 关系。您必须触发额外的查询,意味着:

$result = [];
foreach ($customers->getResults() as $item) {
    $result[] = array_merge(
        $item->toArray(),
        ['groups' => $item->getGroups()->toArray()]
    );
}

return $result;
© www.soinside.com 2019 - 2024. All rights reserved.