Have a look at “lazy loading”. Lazy loading triggers Yii to perform a
relational query on demand:
// Perform one query of the comment table:
$comment = Comment::model()->findByPk(1);
// Run another query to get the associated username:
$user = $comment->user->username;
OR better yet "eager loading"
// Perform one query of the comment table:
$comment = Comment::model()->with('user')->findByPk(1);
// No query necessary to do this:
$user = $comment->user->username;
This does assume I believe that you have set up your relations correctly (which if you did in your DB model and used gii, that should be the case.