Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8519305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:12:44+00:00 2026-06-11T06:12:44+00:00

When linking data in different tables together using foreign ids, I often end up

  • 0

When linking data in different tables together using foreign ids, I often end up writing inelegant code to get the information I need. Here is an example:

Say I have a table Message, with the following fields:

id, touserId, fromuserId, title, message

touserId and fromuserId refer to the id of the User objects that are sending and receiving the message respectively.

Say I want to show all the messages sent to a particular user. I end up writing something like this in my view – which I know is bad!

<?
$messages=Message::model()->findAllByAttributes(array("touserId"=>Yii::app()->user->userid));
foreach ($messages as $message) {   
$fromuser=User::model()->findAllByAttributes(array("id"=>$message->fromId));
?>
<div>
<h4><?=$message->title;?></h4>
<p>From: <?=$fromuser->name'?>
<p><?$message->body;?></p>
</div>
<?
}
?>

Is there a more elegant way to access the information from related records (in this case the name of the user sending the message?)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T06:12:46+00:00Added an answer on June 11, 2026 at 6:12 am

    In your Message model, you can do this:

    public function relations() {
        return array(
            'name' => array(self::HAS_ONE, 'User', 'fromuserId')
        )
    }
    

    Alternatively, you could modify your model to include a JOIN so you get the user names.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking at linking a few applications together (all written in different languages
I used Json to get data off a site build in Wordpress (using the
I am struggling with linking ALAssetsLibrary in my code. I have googled a lot
I am getting linking error when I try to compile a test code. I'm
I was trying out the jQuery Data linking proposal from Microsoft and noticed something
Good morning, I a linking to an Oracle Data (table) through ODBC. I do
Memory allocation in a program: Code and data. Stack. Heap. If I have a
I'm not using deep linking, so all the pages/states appear as http://site.com Is it
In MySQL, is it possible to have a column in two different tables that
I want to scrape html tables using the XML package of R, in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.