phalcon / cphalcon

High performance, full-stack PHP framework delivered as a C extension.
https://phalcon.io
BSD 3-Clause "New" or "Revised" License
10.79k stars 1.96k forks source link

Include the reference model name in the build query when getting rela… #16655

Closed alrieckert closed 1 month ago

alrieckert commented 1 month ago

…ted records

When we overload the getRelated function and join more tables, it can happen that the referencedField/s can't be mapped back correctly to the referenceModel it not specified

Hello!

In raising this pull request, I confirm the following:

Small description of change:

Thanks

alrieckert commented 1 month ago

Before the fix

The column 'id' is ambiguous, when preparing:

SELECT [App\\Models\\Device\\Device].* 
FROM [App\\Models\\Device\\Device] 
JOIN [App\\Models\\User\\User] 
WHERE (([id] = :APR0:) 
    AND (App\\Models\\Role\\EntityToUser.type_id = 10)) 
    AND ([App\\Models\\User\\User].id = :user_id:)"}}

after this patch request

SELECT [App\Models\Device\Device].* 
FROM [App\Models\Device\Device] 
JOIN [App\Models\User\User] 
WHERE (([App\Models\Device\Device].[id] = :APR0:) 
    AND (App\Models\Role\EntityToUser.type_id = 10)) 
    AND ([App\Models\User\User].id = :user_id:)

Now SQL is correctly formatted

Array
(
    [sql] => SELECT 'device'.'device_id', 'device'.'device_serial' 
             FROM 'device'  
             INNER JOIN 'm2m_app'.'entity_to_user' ON 'device'.'device_id' = 'entity_to_user'.'entity_id' 
             INNER JOIN 'm2m_app'.'user' ON 'entity_to_user'.'user_id' = 'user'.'user_id' 
             WHERE (
                 ('device'.'device_id' = :APR0) 
                 AND ('entity_to_user'.'entity_type_id' = 10)
             ) AND ('user'.'user_id' = :user_id)
    [bind] => Array
        (
            [APR0] => 23482
            [user_id] => 1
        )

    [bindTypes] => Array
        (
        )
)
niden commented 1 month ago

@alrieckert If you don't mind, can you add a line to the CHANGELOG regarding this?

After that I will merge it. Thank you so much

niden commented 1 month ago

Thank you @alrieckert