We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Many-to-many column mappings

I have very simple relation, user has many organizations and organization has many users (nothing unusual). But when I want to fetch:

dump($result->user->organizations);

Column 'organization_id' doesn't belong to the model or alias 'App\Model\UserHasOrganization', when executing: SELECT [App\Model\Organization].* FROM [App\Model\Organization] INNER JOIN [App\Model\UserHasOrganization] ON [App\Model\UserHasOrganization].[organization_id] = [App\Model\Organization].[id] WHERE [App\Model\UserHasOrganization].[user_id] = :APR0:

I recognize that when I replace columns mapping, it works:

    public function columnMap()
    {
        return [
            //...
            'organizationId' => 'organization_id'
        ];
    }

But now second side is spoiled:

dump($result->organization->users);

Is a simple way to recognize a relation side and use array_flip to set columnMap dynamic?

If you have n:n relations, then you have to access them as arrays:

foreach($result->organization as $organization) {
    foreach($organization->users as $user) {
        var_dump($user->toArray());
    }
}