I have a table with an ID column, which MySQL defines as the primary key, but also with a varchar column which contains a unique value for each row, example: UserID, UserName… both are unique.
All other tables I have contain the UserName, therefore I would like to set UserName as the primary key. I’ve tried creating a primaryKey function in the model, returning ‘UserName’, and I’ve tried creating an init() function for the model, with $this->setPrimaryKey(‘UserName’); however, generated queries still are created with WHERE (user.UserID=:ypl0).
Can someone tell me how to override the primary key, so queries will be generated with WHERE (user.UserName = …
I am not sure if I understand what you want. You have a table with userId and username and the id is an auto incremented integer I guess. All other tables referencing a user record don’t use the id column as foreign key but the username itself? Well, if you want to reference a user by its username and not the id then you should define the username as primary key in your DATABASE first (otherwise you lose all benefits of referential integrity). Then you should override the primaryKey method as you stated like this
public function primaryKey()
You just have to take care that your relations use the username and not the id value now
Yes, as I said above: Overriding the primaryKey() method is enough. But remember to change the relations method in the other models referencing a User so that they are using ‘username’ and not id as the foreign key
I checks for a pk field in the database via CDbSchema and if none is defined it uses the models primaryKey method, otherwise it uses the database field defined as pk which makes sense in 99% of all cases to not mess things up. So it seems that as long as you define the id field as primary key in your database it will use that one