How do I disambiguate two chained relations that have se same name

I have the User model that has the Profile relation.

And also have the Message model that has the UserFrom and UserTo relations, both pointing to the User model.

Retrieveing Messages with the with clause ‘with’ => array( ‘User1’, ‘User2’ ) is no problem.

But if I want to get those Users’ profiles, for both User1 AND User2, here comes the problem:

‘with’ => array( ‘User1.Profile’, ‘User2.Profile’ )

MySQL cries: SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: ‘Profile’

But it’s not a serious error, just an alias name clash, can I specify each relations aliases, and how?

PS: for now, I can leave the second relation just as ‘User2’ and load its Profile in a lazy way, but it’s not a good solution.

Put another alias in the with array.


Actually didn’t need to change any alias.

If anyone needs this solution, I changed this:

'with' => array( 'User.Profile', 'User2.Profile' ) // which gives an SQL error

into this:

'with' => array( 

  array( 'User', 'Profile' ), 

  array( 'User2', 'Profile' ) ) 

Hi petruza,

i just tried out your solution, but it seems to me that User, User2 and their profiles will be lazy loaded when put into an array like you suggested.

So the with() part is not doing anything…

Did you test your suggestion and are you sure that eager loading works as expected?



I know this post is old but I’ve found it in Google while searching a solution for the same problem.


The solution of Petruza works but it generates a lot SQL queries. Eager loading doesn’t work in this case.

This issue is reported since February 2010 and it might be fixed in the next release ( 1.1.8 ): Issue N°924

In the comments of the issue, Qiang gives the SOLUTION :

In the example of Petruza, replace this:

'with' => array( 'User.Profile', 'User2.Profile' )

with this: (Specify explicitly the alias in order to avoid conflicts)

'with' => array( 'User.Profile', 'User2.Profile'=>array('alias'=>'somethingDifferentThanProfile') )

Thus, the eager loading still works.


Thanks! will try it

Thanks a lot! It works like a magic!