Relation on Non Primary Key

I’ve posted a couple of questions regarding this, and have tried many convoluted solutions, but finally found a very simple one.

I’m trying to link one model to another using fields which are not the primary key. Example: User table with UserId (primary key) and UserName (unique, but not primary key), linked to my table which contains the UserName.

The answer was incredibly simple… In my table create a relation:

‘usertbl’=> array(self::HAS_ONE, ‘UserTable’, array(‘UserName’=>‘UserName’))

I’m sure many will read this and say “well, duh!!!”, but finding this construct took me days, and I’ve seen many posts requesting the same thing, so I thought I’d share.

I was wondering about this too, if I remember correctly I was forced in to creating a foreign key for it to reference the relation properly.