You could miss a lot of thinks in your code so I'll show some of the thing you'll need:
In the models
In your model you need to indicate the relation that exists.
In User
you need to define the relation that link this model to the code
public function relations(){
return array(
'codes'=>array(self::HAS_MANY, 'Code', 'createdby'),
);
}
And in Code
you'll have
public function relations(){
return array(
'author'=>array(self::BELONGS_TO, 'User', 'createdby'),
);
}
Now the models can be linked when callign them in the controllers or the views
The Data Provider
In the data provider we will indicate the releted model that need to be loaded while loading code:
$dataProvider=new CActiveDataProvider('Code', array(
'criteria'=>array(
'with'=>array('author'),
),
));
The View
Now in the view yiou can display the author:
<?php echo CHtml::encode($data->author->getAttributeLabel('username')); ?>:</b>
<?php echo CHtml::encode($data->author->username); ?>