phpsymfonyjoinmany-to-manypropel

Propel Many-to-many join


maybe I'm to stupid for this, but I want to get a many to many result set from my database with propel.

I've a Acl_Customer, Acl_Group and a Acl_Customer_Group table. The last one is the cross-ref between a customer and a group.

Now, I just want to be able to fetch my acl customers with a join on the groups table.

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

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

This brings me the following:

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

But I need something like this:

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

Has anyone an idea?


Solution

  • You can not "join" n-to-n relations in SQL. You have to fire a extra query, means:

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