Skip to content

@property OneHasMany<...> with orderBy lead to duplicate join same table #724

@IvoTomanDev

Description

@IvoTomanDev

Describe the bug
If @Property OneHasMany<...> 1:m has orderBy with sorting by target table, it generate Nextras\Dbal\Drivers\Exception\QueryException ERROR: table name "xxx" specified more than once

To Reproduce

On entity User define

 * @property OneHasMany<UserApplication> $userApplications {1:m UserApplication::$user, orderBy=[application->id=ASC]}

On entity UserApplication

 * @property User $user {m:1 User::$userApplications}
 * @property Application $application {m:1 Application, oneSided=true} 

On Entity Application

 * @property int $id {primary}
 * @property string $name

Use it

		foreach ($user->userApplications as $userApplication) {
			if ($userApplication->application->id === $applicationId) {
				...
			}
		}

Throws exception with this SQL

SELECT "user_application".* 
FROM "core"."user_application" AS "user_application"
LEFT JOIN "core"."application" AS "application" ON ("user_application"."application_id" = "application"."id")
LEFT JOIN "core"."application" AS "application" ON ("user_application"."application_id" = "application"."id")
WHERE "user_application"."user_id" IN (142029)
ORDER BY "application"."id" ASC

Expected behavior
Like before on nextras v 4. Only join table once.

Versions::
PostgreSQL ver. 16.4 (Debian 16.4-1.pgdg120+1)
nextras/dba v5.0.1
nextras/orm v5.0.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions