Auth and user ID issue

Everything was working great in my application till I thought of using RBAC in the application. Below are the changes I made (till this point everything was working fine).

  • Included authManager in main.php configuration file (as per the guide)
  • Manaully imported auth SQL file (yii/framwork/web/auth/schema-mysql.sql) in database
  • Finally executed the code below which got executed successfully as entries were created in auth tables.



$auth=Yii::app()->authManager;


$role = $auth->createRole('admin', 'administrator');

$auth->assign('admin',1); // adding admin to first user created

Right after this, everything stopped working and I get the error below.

[size="3"][color="#ff0000"]Trying to get property of non-object[/color][/size]

[size="2"]This error comes while trying to access $user object. One example mentioned below (error comes at $user access, line 6 below)[/size]





line1: $user = User::model()->findByPk($data->user_id);

line2: echo 'On ' . strftime('%m/%d/%Y at %I:%M %P', strtotime($data->created));

line3: echo ' - ';

line4: echo CHtml::link($data->category->name, array('story/category', 'category_id'=>$data->category_id));

line5: echo ' - ';

line6: echo ' by ' . $user->username . ' (' . CHtml::link($data->gender, array('story/gender', 'gender'=>$data->gender)) . ')';

Since then I have deleted all auth related tables and removed inclusion of authManager in main.php but to no avail. I am 100% sure the problem has started after enabling auth only since morning itself I was testing the application and it was working great.

Anyone aware of any known reason which could be responsible for this? Thanks!

<div><br></div><div><b>UPDATE</b>: Just noticed that both $data and $user objects are not null and have all&nbsp;information&nbsp;which is required in line 6 above. This makes matter more complicated.&nbsp;</div>

It’s trying to add ‘admin’ role to a user whose ID is 1.

Does your user table has an entry whose id is 1 ?

Yes. I made sure there is a record with ID 1.

I figured out the problem and your question helped a lot. I changed the ID of one user to 1 but forgot to update the references of the new ID in database which was causing the issue. So stupid of me :frowning: