Change Primarykey Of Model


I am new to Yii and i want to set the primary key of my Model after creating the Model with "gii". here is the description,

I am using MySql and [size="2"]I have created a model "Profile" with "id" as Auto incremented. [/size][size="2"]But after creating model i found that i should make field "fk_user_id" be the PK so what i did that i have added a function [/size]

[size=“2”]public function primaryKey()[/size][size=“2”]{[/size][size=“2”] return ‘fk_user_id’;[/size][size=“2”]} [/size] [size=“2”]to the model “Profile” but which should return the primary key of the model and it does through echo Profile::model()->getPrimaryKey() in the Profile model which returns ‘fk_user_id’, [/size] [size=“2”]But while performing the join with a Model “Friend” where Friend has a relation [/size] [size=“2”]Relation of model Friend[/size]

public function relations()


return array(

‘user’=>array(self::BELONGS_TO, ‘User’, ‘fk_friend_user_id’),

‘profile’=>array(self::BELONGS_TO, ‘Profile’, ‘fk_friend_user_id’),

‘friendList’ => array(self::MANY_MANY, ‘FriendList’, ‘tbl_friend_friend_list_assoc(fk_friend_id, fk_friend_list_id)’),



after that while fetching the Friend record i am not able to get the related Profile data

[b]public function actionIndex($id)[/b]


$criteria=new CDbCriteria;

$criteria->with = array("profile");

$criteria->condition = "t.fk_user_id=".$id;

$dataProvider=new CActiveDataProvider(‘Friend’, array(







I have noticed that if i make the fk_user_id as primary key in the table structure its getting the related Profile data!

i have attached the table schema screen shots

Can someone help me ?

Thanks in advance

check out this

Is ‘fk_friend_user_id’ the actual name of the field/attribute?




where to write the init() method ?

[color="#1C2837"] [/color]

One of the reasons for init() is about life cycles of an object (or a component to be exact).

With an init() method, it is possible to configure an object after it is instantiated while before fully initialized. For example, an application component could be configured using app config. If you override its init() method, you will be sure that the configuration is applied and you can safely to check if everything is ready. Similar thing happens to a widget and other configurable components.

Even if init() is called within constructor rather than by another object, it has meaning. For example, in CApplication, there are preInit() and init(). They set up the life cycles of an application and may be overridden so that the customization only occurs at expected life cycles.

I agree with you that the method naming is very important. Here in Yii, init() method means that an object is already fully configured and some additional initialization work should be done in this method.

[color="#1C2837"][size="2"]It Should be written in class file which extends (CApplication)[/size][/color]

It was just a strange name for a table field. Usually that naming it the actual foreign key index name. That’s why I asked.

Yes, i usually feel good to refer the the foreign key relationship with this convention. Do u have any other idea to name it ?

For foreign key relations in the server this is a correct convention. Yii relations refer to an actual field name in the table. Usually it would just be user_id, or friend_id, but I guess your way indicates that this field is part of a foreign key.

From an ‘I just got hired to work on this project’ point-of-view, it just looked strange.

Yes, yii does like the way you said but i felt it often confusing so i did like that.

As i am new to yii but i have exp in cakePHP so i feel bit slow to get the pace in code writing. Do you have idea in cakePHP ?

Never used it.

The one thing about coding conventions: pick one and stick to it. If you don’t need to worry about anybody else (ie; a team member) working your code, then do what you want.

Yes, i completely agree with you. From how many years you are using yii ?

programming: 30+

Html: 20+

Yii: 3+

waooo thats great, i have just started my career with having just 2yrs of exp.