【问题标题】:Propel Many-to-many join推进多对多连接
【发布时间】:2014-01-27 11:34:13
【问题描述】:

也许我对此很愚蠢,但我想用推进器从我的数据库中获得多对多的结果集。

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

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

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

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

这给我带来了以下信息:

[{"id":1,"username":"foo","email":"bar@baz.quux"}]

但我需要这样的东西:

[{"id":1,"username":"foo","email":"bar@baz.quux","groups":[{"name":"developer"}, ...]}]

有人有想法吗?

【问题讨论】:

    标签: symfony join many-to-many propel


    【解决方案1】:

    您不能在 SQL 中“加入”n 对 n 关系。您必须触发一个额外的查询,这意味着:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-27
      • 2017-07-16
      • 1970-01-01
      • 2022-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多