cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

[BUG] [1.3.2] aliased Many-To-Many relation doesn't create correct JOIN query

Open HitoriSensei opened this issue 12 years ago • 3 comments

I have two models in relation:

Models\Uzytkownicy

$this->hasManyToMany(
    'uzytkownik_id',
    'Models\UzytkownicyRole',
    'uzytkownik_id_uzytkownicy_dane', 'rola_id_role',
    'Models\Role',
    'rola_id',
    [ 'alias' => 'UzytkownikRole' ]
);

Models\Role

$this->hasManyToMany(
    'rola_id',
    'Models\UzytkownicyRole',
    'rola_id_role', 'uzytkownik_id_uzytkownicy_dane',
    'Models\Uzytkownicy',
    'uzytkownik_id',
    [ 'alias' => 'RolaUzytkownicy' ]
);

And an intermediate model: Models\UzytkownicyRole

$this->belongsTo('rola_id_role', 'Models\Role', 'rola_id' , [ 'alias' => 'UzytkownikRola' ]);
$this->belongsTo('uzytkownik_id_uzytkownicy_dane', 'Models\Uzytkownicy', 'uzytkownik_id', [ 'alias' => 'UzytkownikRoli' ] );

When I try to execute a query:

SELECT Models\Role.*
from Models\Role
JOIN Models\Uzytkownicy
where Models\Uzytkownicy.login = "erkerk"

it creates incorrect raw query without statements for joining intermediate table:

SELECT "role"."rola_id", "role"."nazwa", "role"."stala"
FROM "public"."role"
INNER JOIN "public"."uzytkownicy"
WHERE "uzytkownicy"."login" = 'erkerk'

But, when I remove the alias from Models\Role Many-To-Many relation declaration:

$this->hasManyToMany(
    'rola_id',
    'Models\UzytkownicyRole',
    'rola_id_role', 'uzytkownik_id_uzytkownicy_dane',
    'Models\Uzytkownicy',
    'uzytkownik_id'
    // [ 'alias' => 'RolaUzytkownicy' ]
);

and execute the same PHQL query as above, it generates correct raw query:

SELECT "role"."rola_id", "role"."nazwa", "role"."stala" 
FROM "public"."role"
INNER JOIN "many_role_has_many_uzytkownicy_dane" ON "role"."rola_id" = "many_role_has_many_uzytkownicy_dane"."rola_id_role"
INNER JOIN "public"."uzytkownicy" ON "many_role_has_many_uzytkownicy_dane"."uzytkownik_id_uzytkownicy_dane" = "uzytkownicy"."uzytkownik_id"
WHERE "uzytkownicy"."login" = 'erkerk'

I'm using Postgresql database adapter. This bug also exists in earlier versions (tested down to 1.2.6).

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

HitoriSensei avatar Jun 26 '14 13:06 HitoriSensei

Hey, @phalcon, did you took a look at this one? I'm using 1.3.4 version with MySQL and getting the same weird behavior.

@HitoriSensei I guess you can close #2567 and keep just this issue. And oh, maybe things get clearer if you translate your models, because "users" is much easier than "uzytkownicy" :smile:

Edit: I've just posted about this in Phalcon forum: http://forum.phalconphp.com/discussion/4339/aliased-manytomany-relation-doesnt-create-correct-join-query.

lfbittencourt avatar Dec 27 '14 16:12 lfbittencourt

+1 i can reconfirm this bug for version 1.3.3

Edit: Not fixed in branch 1.3.5 (which still shows as 1.3.4 in Phalcon\Version) nor in branch 2.0.0

Edit2: One possible workaround is to not define the alias and resolve Models\Role by calling ->getRelated('Models\Role') from a magic __get method in Models\Uzytkownicy.

loopsframework avatar Feb 06 '15 07:02 loopsframework

Thank you for contributing to this issue. As it has been 90 days since the last activity, we are automatically closing the issue. This is often because the request was already solved in some way and it just wasn't updated or it's no longer applicable. If that's not the case, please feel free to either reopen this issue or open a new one. We will be more than happy to look at it again! You can read more here: https://blog.phalconphp.com/post/github-closing-old-issues

stale[bot] avatar Apr 17 '18 05:04 stale[bot]